added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / serial / 8250.c
bloba7031102c7d99cc6ea6bfc6ea46e5201f1193458
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 * A note about mapbase / membase
17 * mapbase is the physical address of the IO port.
18 * membase is an 'ioremapped' cookie.
21 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22 #define SUPPORT_SYSRQ
23 #endif
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/ioport.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/sysrq.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/rt_lock.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/serial_reg.h>
37 #include <linux/serial_core.h>
38 #include <linux/serial.h>
39 #include <linux/serial_8250.h>
40 #include <linux/nmi.h>
41 #include <linux/mutex.h>
43 #include <asm/io.h>
44 #include <asm/irq.h>
46 #include "8250.h"
48 #ifdef CONFIG_SPARC
49 #include "suncore.h"
50 #endif
53 * Configuration:
54 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
55 * is unsafe when used on edge-triggered interrupts.
57 static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
59 static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
61 static struct uart_driver serial8250_reg;
63 static int serial_index(struct uart_port *port)
65 return (serial8250_reg.minor - 64) + port->line;
69 * Debugging.
71 #if 0
72 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
73 #else
74 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
75 #endif
77 #if 0
78 #define DEBUG_INTR(fmt...) printk(fmt)
79 #else
80 #define DEBUG_INTR(fmt...) do { } while (0)
81 #endif
83 #define PASS_LIMIT 256
86 * We default to IRQ0 for the "no irq" hack. Some
87 * machine types want others as well - they're free
88 * to redefine this in their header file.
90 #define is_real_interrupt(irq) ((irq) != 0)
92 #ifdef CONFIG_SERIAL_8250_DETECT_IRQ
93 #define CONFIG_SERIAL_DETECT_IRQ 1
94 #endif
95 #ifdef CONFIG_SERIAL_8250_MANY_PORTS
96 #define CONFIG_SERIAL_MANY_PORTS 1
97 #endif
100 * HUB6 is always on. This will be removed once the header
101 * files have been cleaned.
103 #define CONFIG_HUB6 1
105 #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
113 #endif
115 static const struct old_serial_port old_serial_port[] = {
116 SERIAL_PORT_DFNS /* defined in asm/serial.h */
119 #define UART_NR 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 short capabilities; /* port capabilities */
133 unsigned short bugs; /* port bugs */
134 unsigned int tx_loadsz; /* transmit fifo load size */
135 unsigned char acr;
136 unsigned char ier;
137 unsigned char lcr;
138 unsigned char mcr;
139 unsigned char mcr_mask; /* mask of user bits */
140 unsigned char mcr_force; /* mask of forced bits */
141 unsigned char cur_iotype; /* Running I/O type */
144 * Some bits in registers are cleared on a read, so they must
145 * be saved whenever the register is read but the bits will not
146 * be immediately processed.
148 #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
149 unsigned char lsr_saved_flags;
150 #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
151 unsigned char msr_saved_flags;
154 * We provide a per-port pm hook.
156 void (*pm)(struct uart_port *port,
157 unsigned int state, unsigned int old);
160 struct irq_info {
161 struct hlist_node node;
162 int irq;
163 spinlock_t lock; /* Protects list not the hash */
164 struct list_head *head;
167 #define NR_IRQ_HASH 32 /* Can be adjusted later */
168 static struct hlist_head irq_lists[NR_IRQ_HASH];
169 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
172 * Here we define the default xmit fifo size used for each type of UART.
174 static const struct serial8250_config uart_config[] = {
175 [PORT_UNKNOWN] = {
176 .name = "unknown",
177 .fifo_size = 1,
178 .tx_loadsz = 1,
180 [PORT_8250] = {
181 .name = "8250",
182 .fifo_size = 1,
183 .tx_loadsz = 1,
185 [PORT_16450] = {
186 .name = "16450",
187 .fifo_size = 1,
188 .tx_loadsz = 1,
190 [PORT_16550] = {
191 .name = "16550",
192 .fifo_size = 1,
193 .tx_loadsz = 1,
195 [PORT_16550A] = {
196 .name = "16550A",
197 .fifo_size = 16,
198 .tx_loadsz = 16,
199 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
200 .flags = UART_CAP_FIFO,
202 [PORT_CIRRUS] = {
203 .name = "Cirrus",
204 .fifo_size = 1,
205 .tx_loadsz = 1,
207 [PORT_16650] = {
208 .name = "ST16650",
209 .fifo_size = 1,
210 .tx_loadsz = 1,
211 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
213 [PORT_16650V2] = {
214 .name = "ST16650V2",
215 .fifo_size = 32,
216 .tx_loadsz = 16,
217 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
218 UART_FCR_T_TRIG_00,
219 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
221 [PORT_16750] = {
222 .name = "TI16750",
223 .fifo_size = 64,
224 .tx_loadsz = 64,
225 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
226 UART_FCR7_64BYTE,
227 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
229 [PORT_STARTECH] = {
230 .name = "Startech",
231 .fifo_size = 1,
232 .tx_loadsz = 1,
234 [PORT_16C950] = {
235 .name = "16C950/954",
236 .fifo_size = 128,
237 .tx_loadsz = 128,
238 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
239 .flags = UART_CAP_FIFO,
241 [PORT_16654] = {
242 .name = "ST16654",
243 .fifo_size = 64,
244 .tx_loadsz = 32,
245 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
246 UART_FCR_T_TRIG_10,
247 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
249 [PORT_16850] = {
250 .name = "XR16850",
251 .fifo_size = 128,
252 .tx_loadsz = 128,
253 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
254 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
256 [PORT_RSA] = {
257 .name = "RSA",
258 .fifo_size = 2048,
259 .tx_loadsz = 2048,
260 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
261 .flags = UART_CAP_FIFO,
263 [PORT_NS16550A] = {
264 .name = "NS16550A",
265 .fifo_size = 16,
266 .tx_loadsz = 16,
267 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
268 .flags = UART_CAP_FIFO | UART_NATSEMI,
270 [PORT_XSCALE] = {
271 .name = "XScale",
272 .fifo_size = 32,
273 .tx_loadsz = 32,
274 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
275 .flags = UART_CAP_FIFO | UART_CAP_UUE,
277 [PORT_RM9000] = {
278 .name = "RM9000",
279 .fifo_size = 16,
280 .tx_loadsz = 16,
281 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
282 .flags = UART_CAP_FIFO,
284 [PORT_OCTEON] = {
285 .name = "OCTEON",
286 .fifo_size = 64,
287 .tx_loadsz = 64,
288 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
289 .flags = UART_CAP_FIFO,
293 #if defined (CONFIG_SERIAL_8250_AU1X00)
295 /* Au1x00 UART hardware has a weird register layout */
296 static const u8 au_io_in_map[] = {
297 [UART_RX] = 0,
298 [UART_IER] = 2,
299 [UART_IIR] = 3,
300 [UART_LCR] = 5,
301 [UART_MCR] = 6,
302 [UART_LSR] = 7,
303 [UART_MSR] = 8,
306 static const u8 au_io_out_map[] = {
307 [UART_TX] = 1,
308 [UART_IER] = 2,
309 [UART_FCR] = 4,
310 [UART_LCR] = 5,
311 [UART_MCR] = 6,
314 /* sane hardware needs no mapping */
315 static inline int map_8250_in_reg(struct uart_port *p, int offset)
317 if (p->iotype != UPIO_AU)
318 return offset;
319 return au_io_in_map[offset];
322 static inline int map_8250_out_reg(struct uart_port *p, int offset)
324 if (p->iotype != UPIO_AU)
325 return offset;
326 return au_io_out_map[offset];
329 #elif defined(CONFIG_SERIAL_8250_RM9K)
331 static const u8
332 regmap_in[8] = {
333 [UART_RX] = 0x00,
334 [UART_IER] = 0x0c,
335 [UART_IIR] = 0x14,
336 [UART_LCR] = 0x1c,
337 [UART_MCR] = 0x20,
338 [UART_LSR] = 0x24,
339 [UART_MSR] = 0x28,
340 [UART_SCR] = 0x2c
342 regmap_out[8] = {
343 [UART_TX] = 0x04,
344 [UART_IER] = 0x0c,
345 [UART_FCR] = 0x18,
346 [UART_LCR] = 0x1c,
347 [UART_MCR] = 0x20,
348 [UART_LSR] = 0x24,
349 [UART_MSR] = 0x28,
350 [UART_SCR] = 0x2c
353 static inline int map_8250_in_reg(struct uart_port *p, int offset)
355 if (p->iotype != UPIO_RM9000)
356 return offset;
357 return regmap_in[offset];
360 static inline int map_8250_out_reg(struct uart_port *p, int offset)
362 if (p->iotype != UPIO_RM9000)
363 return offset;
364 return regmap_out[offset];
367 #else
369 /* sane hardware needs no mapping */
370 #define map_8250_in_reg(up, offset) (offset)
371 #define map_8250_out_reg(up, offset) (offset)
373 #endif
375 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
377 offset = map_8250_in_reg(p, offset) << p->regshift;
378 outb(p->hub6 - 1 + offset, p->iobase);
379 return inb(p->iobase + 1);
382 static void hub6_serial_out(struct uart_port *p, int offset, int value)
384 offset = map_8250_out_reg(p, offset) << p->regshift;
385 outb(p->hub6 - 1 + offset, p->iobase);
386 outb(value, p->iobase + 1);
389 static unsigned int mem_serial_in(struct uart_port *p, int offset)
391 offset = map_8250_in_reg(p, offset) << p->regshift;
392 return readb(p->membase + offset);
395 static void mem_serial_out(struct uart_port *p, int offset, int value)
397 offset = map_8250_out_reg(p, offset) << p->regshift;
398 writeb(value, p->membase + offset);
401 static void mem32_serial_out(struct uart_port *p, int offset, int value)
403 offset = map_8250_out_reg(p, offset) << p->regshift;
404 writel(value, p->membase + offset);
407 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
409 offset = map_8250_in_reg(p, offset) << p->regshift;
410 return readl(p->membase + offset);
413 #ifdef CONFIG_SERIAL_8250_AU1X00
414 static unsigned int au_serial_in(struct uart_port *p, int offset)
416 offset = map_8250_in_reg(p, offset) << p->regshift;
417 return __raw_readl(p->membase + offset);
420 static void au_serial_out(struct uart_port *p, int offset, int value)
422 offset = map_8250_out_reg(p, offset) << p->regshift;
423 __raw_writel(value, p->membase + offset);
425 #endif
427 static unsigned int tsi_serial_in(struct uart_port *p, int offset)
429 unsigned int tmp;
430 offset = map_8250_in_reg(p, offset) << p->regshift;
431 if (offset == UART_IIR) {
432 tmp = readl(p->membase + (UART_IIR & ~3));
433 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
434 } else
435 return readb(p->membase + offset);
438 static void tsi_serial_out(struct uart_port *p, int offset, int value)
440 offset = map_8250_out_reg(p, offset) << p->regshift;
441 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
442 writeb(value, p->membase + offset);
445 static void dwapb_serial_out(struct uart_port *p, int offset, int value)
447 int save_offset = offset;
448 offset = map_8250_out_reg(p, offset) << p->regshift;
449 /* Save the LCR value so it can be re-written when a
450 * Busy Detect interrupt occurs. */
451 if (save_offset == UART_LCR) {
452 struct uart_8250_port *up = (struct uart_8250_port *)p;
453 up->lcr = value;
455 writeb(value, p->membase + offset);
456 /* Read the IER to ensure any interrupt is cleared before
457 * returning from ISR. */
458 if (save_offset == UART_TX || save_offset == UART_IER)
459 value = p->serial_in(p, UART_IER);
462 static unsigned int io_serial_in(struct uart_port *p, int offset)
464 offset = map_8250_in_reg(p, offset) << p->regshift;
465 return inb(p->iobase + offset);
468 static void io_serial_out(struct uart_port *p, int offset, int value)
470 offset = map_8250_out_reg(p, offset) << p->regshift;
471 outb(value, p->iobase + offset);
474 static void set_io_from_upio(struct uart_port *p)
476 struct uart_8250_port *up = (struct uart_8250_port *)p;
477 switch (p->iotype) {
478 case UPIO_HUB6:
479 p->serial_in = hub6_serial_in;
480 p->serial_out = hub6_serial_out;
481 break;
483 case UPIO_MEM:
484 p->serial_in = mem_serial_in;
485 p->serial_out = mem_serial_out;
486 break;
488 case UPIO_RM9000:
489 case UPIO_MEM32:
490 p->serial_in = mem32_serial_in;
491 p->serial_out = mem32_serial_out;
492 break;
494 #ifdef CONFIG_SERIAL_8250_AU1X00
495 case UPIO_AU:
496 p->serial_in = au_serial_in;
497 p->serial_out = au_serial_out;
498 break;
499 #endif
500 case UPIO_TSI:
501 p->serial_in = tsi_serial_in;
502 p->serial_out = tsi_serial_out;
503 break;
505 case UPIO_DWAPB:
506 p->serial_in = mem_serial_in;
507 p->serial_out = dwapb_serial_out;
508 break;
510 default:
511 p->serial_in = io_serial_in;
512 p->serial_out = io_serial_out;
513 break;
515 /* Remember loaded iotype */
516 up->cur_iotype = p->iotype;
519 static void
520 serial_out_sync(struct uart_8250_port *up, int offset, int value)
522 struct uart_port *p = &up->port;
523 switch (p->iotype) {
524 case UPIO_MEM:
525 case UPIO_MEM32:
526 #ifdef CONFIG_SERIAL_8250_AU1X00
527 case UPIO_AU:
528 #endif
529 case UPIO_DWAPB:
530 p->serial_out(p, offset, value);
531 p->serial_in(p, UART_LCR); /* safe, no side-effects */
532 break;
533 default:
534 p->serial_out(p, offset, value);
538 #define serial_in(up, offset) \
539 (up->port.serial_in(&(up)->port, (offset)))
540 #define serial_out(up, offset, value) \
541 (up->port.serial_out(&(up)->port, (offset), (value)))
543 * We used to support using pause I/O for certain machines. We
544 * haven't supported this for a while, but just in case it's badly
545 * needed for certain old 386 machines, I've left these #define's
546 * in....
548 #define serial_inp(up, offset) serial_in(up, offset)
549 #define serial_outp(up, offset, value) serial_out(up, offset, value)
551 /* Uart divisor latch read */
552 static inline int _serial_dl_read(struct uart_8250_port *up)
554 return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8;
557 /* Uart divisor latch write */
558 static inline void _serial_dl_write(struct uart_8250_port *up, int value)
560 serial_outp(up, UART_DLL, value & 0xff);
561 serial_outp(up, UART_DLM, value >> 8 & 0xff);
564 #if defined(CONFIG_SERIAL_8250_AU1X00)
565 /* Au1x00 haven't got a standard divisor latch */
566 static int serial_dl_read(struct uart_8250_port *up)
568 if (up->port.iotype == UPIO_AU)
569 return __raw_readl(up->port.membase + 0x28);
570 else
571 return _serial_dl_read(up);
574 static void serial_dl_write(struct uart_8250_port *up, int value)
576 if (up->port.iotype == UPIO_AU)
577 __raw_writel(value, up->port.membase + 0x28);
578 else
579 _serial_dl_write(up, value);
581 #elif defined(CONFIG_SERIAL_8250_RM9K)
582 static int serial_dl_read(struct uart_8250_port *up)
584 return (up->port.iotype == UPIO_RM9000) ?
585 (((__raw_readl(up->port.membase + 0x10) << 8) |
586 (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) :
587 _serial_dl_read(up);
590 static void serial_dl_write(struct uart_8250_port *up, int value)
592 if (up->port.iotype == UPIO_RM9000) {
593 __raw_writel(value, up->port.membase + 0x08);
594 __raw_writel(value >> 8, up->port.membase + 0x10);
595 } else {
596 _serial_dl_write(up, value);
599 #else
600 #define serial_dl_read(up) _serial_dl_read(up)
601 #define serial_dl_write(up, value) _serial_dl_write(up, value)
602 #endif
605 * For the 16C950
607 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
609 serial_out(up, UART_SCR, offset);
610 serial_out(up, UART_ICR, value);
613 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
615 unsigned int value;
617 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
618 serial_out(up, UART_SCR, offset);
619 value = serial_in(up, UART_ICR);
620 serial_icr_write(up, UART_ACR, up->acr);
622 return value;
626 * FIFO support.
628 static void serial8250_clear_fifos(struct uart_8250_port *p)
630 if (p->capabilities & UART_CAP_FIFO) {
631 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO);
632 serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO |
633 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
634 serial_outp(p, UART_FCR, 0);
639 * IER sleep support. UARTs which have EFRs need the "extended
640 * capability" bit enabled. Note that on XR16C850s, we need to
641 * reset LCR to write to IER.
643 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
645 if (p->capabilities & UART_CAP_SLEEP) {
646 if (p->capabilities & UART_CAP_EFR) {
647 serial_outp(p, UART_LCR, 0xBF);
648 serial_outp(p, UART_EFR, UART_EFR_ECB);
649 serial_outp(p, UART_LCR, 0);
651 serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
652 if (p->capabilities & UART_CAP_EFR) {
653 serial_outp(p, UART_LCR, 0xBF);
654 serial_outp(p, UART_EFR, 0);
655 serial_outp(p, UART_LCR, 0);
660 #ifdef CONFIG_SERIAL_8250_RSA
662 * Attempts to turn on the RSA FIFO. Returns zero on failure.
663 * We set the port uart clock rate if we succeed.
665 static int __enable_rsa(struct uart_8250_port *up)
667 unsigned char mode;
668 int result;
670 mode = serial_inp(up, UART_RSA_MSR);
671 result = mode & UART_RSA_MSR_FIFO;
673 if (!result) {
674 serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
675 mode = serial_inp(up, UART_RSA_MSR);
676 result = mode & UART_RSA_MSR_FIFO;
679 if (result)
680 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
682 return result;
685 static void enable_rsa(struct uart_8250_port *up)
687 if (up->port.type == PORT_RSA) {
688 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
689 spin_lock_irq(&up->port.lock);
690 __enable_rsa(up);
691 spin_unlock_irq(&up->port.lock);
693 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
694 serial_outp(up, UART_RSA_FRR, 0);
699 * Attempts to turn off the RSA FIFO. Returns zero on failure.
700 * It is unknown why interrupts were disabled in here. However,
701 * the caller is expected to preserve this behaviour by grabbing
702 * the spinlock before calling this function.
704 static void disable_rsa(struct uart_8250_port *up)
706 unsigned char mode;
707 int result;
709 if (up->port.type == PORT_RSA &&
710 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
711 spin_lock_irq(&up->port.lock);
713 mode = serial_inp(up, UART_RSA_MSR);
714 result = !(mode & UART_RSA_MSR_FIFO);
716 if (!result) {
717 serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
718 mode = serial_inp(up, UART_RSA_MSR);
719 result = !(mode & UART_RSA_MSR_FIFO);
722 if (result)
723 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
724 spin_unlock_irq(&up->port.lock);
727 #endif /* CONFIG_SERIAL_8250_RSA */
730 * This is a quickie test to see how big the FIFO is.
731 * It doesn't work at all the time, more's the pity.
733 static int size_fifo(struct uart_8250_port *up)
735 unsigned char old_fcr, old_mcr, old_lcr;
736 unsigned short old_dl;
737 int count;
739 old_lcr = serial_inp(up, UART_LCR);
740 serial_outp(up, UART_LCR, 0);
741 old_fcr = serial_inp(up, UART_FCR);
742 old_mcr = serial_inp(up, UART_MCR);
743 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
744 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
745 serial_outp(up, UART_MCR, UART_MCR_LOOP);
746 serial_outp(up, UART_LCR, UART_LCR_DLAB);
747 old_dl = serial_dl_read(up);
748 serial_dl_write(up, 0x0001);
749 serial_outp(up, UART_LCR, 0x03);
750 for (count = 0; count < 256; count++)
751 serial_outp(up, UART_TX, count);
752 mdelay(20);/* FIXME - schedule_timeout */
753 for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) &&
754 (count < 256); count++)
755 serial_inp(up, UART_RX);
756 serial_outp(up, UART_FCR, old_fcr);
757 serial_outp(up, UART_MCR, old_mcr);
758 serial_outp(up, UART_LCR, UART_LCR_DLAB);
759 serial_dl_write(up, old_dl);
760 serial_outp(up, UART_LCR, old_lcr);
762 return count;
766 * Read UART ID using the divisor method - set DLL and DLM to zero
767 * and the revision will be in DLL and device type in DLM. We
768 * preserve the device state across this.
770 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
772 unsigned char old_dll, old_dlm, old_lcr;
773 unsigned int id;
775 old_lcr = serial_inp(p, UART_LCR);
776 serial_outp(p, UART_LCR, UART_LCR_DLAB);
778 old_dll = serial_inp(p, UART_DLL);
779 old_dlm = serial_inp(p, UART_DLM);
781 serial_outp(p, UART_DLL, 0);
782 serial_outp(p, UART_DLM, 0);
784 id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8;
786 serial_outp(p, UART_DLL, old_dll);
787 serial_outp(p, UART_DLM, old_dlm);
788 serial_outp(p, UART_LCR, old_lcr);
790 return id;
794 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
795 * When this function is called we know it is at least a StarTech
796 * 16650 V2, but it might be one of several StarTech UARTs, or one of
797 * its clones. (We treat the broken original StarTech 16650 V1 as a
798 * 16550, and why not? Startech doesn't seem to even acknowledge its
799 * existence.)
801 * What evil have men's minds wrought...
803 static void autoconfig_has_efr(struct uart_8250_port *up)
805 unsigned int id1, id2, id3, rev;
808 * Everything with an EFR has SLEEP
810 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
813 * First we check to see if it's an Oxford Semiconductor UART.
815 * If we have to do this here because some non-National
816 * Semiconductor clone chips lock up if you try writing to the
817 * LSR register (which serial_icr_read does)
821 * Check for Oxford Semiconductor 16C950.
823 * EFR [4] must be set else this test fails.
825 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
826 * claims that it's needed for 952 dual UART's (which are not
827 * recommended for new designs).
829 up->acr = 0;
830 serial_out(up, UART_LCR, 0xBF);
831 serial_out(up, UART_EFR, UART_EFR_ECB);
832 serial_out(up, UART_LCR, 0x00);
833 id1 = serial_icr_read(up, UART_ID1);
834 id2 = serial_icr_read(up, UART_ID2);
835 id3 = serial_icr_read(up, UART_ID3);
836 rev = serial_icr_read(up, UART_REV);
838 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
840 if (id1 == 0x16 && id2 == 0xC9 &&
841 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
842 up->port.type = PORT_16C950;
845 * Enable work around for the Oxford Semiconductor 952 rev B
846 * chip which causes it to seriously miscalculate baud rates
847 * when DLL is 0.
849 if (id3 == 0x52 && rev == 0x01)
850 up->bugs |= UART_BUG_QUOT;
851 return;
855 * We check for a XR16C850 by setting DLL and DLM to 0, and then
856 * reading back DLL and DLM. The chip type depends on the DLM
857 * value read back:
858 * 0x10 - XR16C850 and the DLL contains the chip revision.
859 * 0x12 - XR16C2850.
860 * 0x14 - XR16C854.
862 id1 = autoconfig_read_divisor_id(up);
863 DEBUG_AUTOCONF("850id=%04x ", id1);
865 id2 = id1 >> 8;
866 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
867 up->port.type = PORT_16850;
868 return;
872 * It wasn't an XR16C850.
874 * We distinguish between the '654 and the '650 by counting
875 * how many bytes are in the FIFO. I'm using this for now,
876 * since that's the technique that was sent to me in the
877 * serial driver update, but I'm not convinced this works.
878 * I've had problems doing this in the past. -TYT
880 if (size_fifo(up) == 64)
881 up->port.type = PORT_16654;
882 else
883 up->port.type = PORT_16650V2;
887 * We detected a chip without a FIFO. Only two fall into
888 * this category - the original 8250 and the 16450. The
889 * 16450 has a scratch register (accessible with LCR=0)
891 static void autoconfig_8250(struct uart_8250_port *up)
893 unsigned char scratch, status1, status2;
895 up->port.type = PORT_8250;
897 scratch = serial_in(up, UART_SCR);
898 serial_outp(up, UART_SCR, 0xa5);
899 status1 = serial_in(up, UART_SCR);
900 serial_outp(up, UART_SCR, 0x5a);
901 status2 = serial_in(up, UART_SCR);
902 serial_outp(up, UART_SCR, scratch);
904 if (status1 == 0xa5 && status2 == 0x5a)
905 up->port.type = PORT_16450;
908 static int broken_efr(struct uart_8250_port *up)
911 * Exar ST16C2550 "A2" devices incorrectly detect as
912 * having an EFR, and report an ID of 0x0201. See
913 * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf
915 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
916 return 1;
918 return 0;
922 * We know that the chip has FIFOs. Does it have an EFR? The
923 * EFR is located in the same register position as the IIR and
924 * we know the top two bits of the IIR are currently set. The
925 * EFR should contain zero. Try to read the EFR.
927 static void autoconfig_16550a(struct uart_8250_port *up)
929 unsigned char status1, status2;
930 unsigned int iersave;
932 up->port.type = PORT_16550A;
933 up->capabilities |= UART_CAP_FIFO;
936 * Check for presence of the EFR when DLAB is set.
937 * Only ST16C650V1 UARTs pass this test.
939 serial_outp(up, UART_LCR, UART_LCR_DLAB);
940 if (serial_in(up, UART_EFR) == 0) {
941 serial_outp(up, UART_EFR, 0xA8);
942 if (serial_in(up, UART_EFR) != 0) {
943 DEBUG_AUTOCONF("EFRv1 ");
944 up->port.type = PORT_16650;
945 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
946 } else {
947 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
949 serial_outp(up, UART_EFR, 0);
950 return;
954 * Maybe it requires 0xbf to be written to the LCR.
955 * (other ST16C650V2 UARTs, TI16C752A, etc)
957 serial_outp(up, UART_LCR, 0xBF);
958 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
959 DEBUG_AUTOCONF("EFRv2 ");
960 autoconfig_has_efr(up);
961 return;
965 * Check for a National Semiconductor SuperIO chip.
966 * Attempt to switch to bank 2, read the value of the LOOP bit
967 * from EXCR1. Switch back to bank 0, change it in MCR. Then
968 * switch back to bank 2, read it from EXCR1 again and check
969 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
971 serial_outp(up, UART_LCR, 0);
972 status1 = serial_in(up, UART_MCR);
973 serial_outp(up, UART_LCR, 0xE0);
974 status2 = serial_in(up, 0x02); /* EXCR1 */
976 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
977 serial_outp(up, UART_LCR, 0);
978 serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP);
979 serial_outp(up, UART_LCR, 0xE0);
980 status2 = serial_in(up, 0x02); /* EXCR1 */
981 serial_outp(up, UART_LCR, 0);
982 serial_outp(up, UART_MCR, status1);
984 #if 0
985 if ((status2 ^ status1) & UART_MCR_LOOP) {
986 unsigned short quot;
988 serial_outp(up, UART_LCR, 0xE0);
990 quot = serial_dl_read(up);
991 quot <<= 3;
993 status1 = serial_in(up, 0x04); /* EXCR2 */
994 status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
995 status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
996 serial_outp(up, 0x04, status1);
998 serial_dl_write(up, quot);
1000 serial_outp(up, UART_LCR, 0);
1002 up->port.uartclk = 921600*16;
1003 up->port.type = PORT_NS16550A;
1004 up->capabilities |= UART_NATSEMI;
1005 return;
1007 #endif
1011 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1012 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1013 * Try setting it with and without DLAB set. Cheap clones
1014 * set bit 5 without DLAB set.
1016 serial_outp(up, UART_LCR, 0);
1017 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1018 status1 = serial_in(up, UART_IIR) >> 5;
1019 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1020 serial_outp(up, UART_LCR, UART_LCR_DLAB);
1021 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1022 status2 = serial_in(up, UART_IIR) >> 5;
1023 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1024 serial_outp(up, UART_LCR, 0);
1026 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1028 if (status1 == 6 && status2 == 7) {
1029 up->port.type = PORT_16750;
1030 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1031 return;
1035 * Try writing and reading the UART_IER_UUE bit (b6).
1036 * If it works, this is probably one of the Xscale platform's
1037 * internal UARTs.
1038 * We're going to explicitly set the UUE bit to 0 before
1039 * trying to write and read a 1 just to make sure it's not
1040 * already a 1 and maybe locked there before we even start start.
1042 iersave = serial_in(up, UART_IER);
1043 serial_outp(up, UART_IER, iersave & ~UART_IER_UUE);
1044 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1046 * OK it's in a known zero state, try writing and reading
1047 * without disturbing the current state of the other bits.
1049 serial_outp(up, UART_IER, iersave | UART_IER_UUE);
1050 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1052 * It's an Xscale.
1053 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1055 DEBUG_AUTOCONF("Xscale ");
1056 up->port.type = PORT_XSCALE;
1057 up->capabilities |= UART_CAP_UUE;
1058 return;
1060 } else {
1062 * If we got here we couldn't force the IER_UUE bit to 0.
1063 * Log it and continue.
1065 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1067 serial_outp(up, UART_IER, iersave);
1071 * This routine is called by rs_init() to initialize a specific serial
1072 * port. It determines what type of UART chip this serial port is
1073 * using: 8250, 16450, 16550, 16550A. The important question is
1074 * whether or not this UART is a 16550A or not, since this will
1075 * determine whether or not we can use its FIFO features or not.
1077 static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
1079 unsigned char status1, scratch, scratch2, scratch3;
1080 unsigned char save_lcr, save_mcr;
1081 unsigned long flags;
1083 if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
1084 return;
1086 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ",
1087 serial_index(&up->port), up->port.iobase, up->port.membase);
1090 * We really do need global IRQs disabled here - we're going to
1091 * be frobbing the chips IRQ enable register to see if it exists.
1093 spin_lock_irqsave(&up->port.lock, flags);
1095 up->capabilities = 0;
1096 up->bugs = 0;
1098 if (!(up->port.flags & UPF_BUGGY_UART)) {
1100 * Do a simple existence test first; if we fail this,
1101 * there's no point trying anything else.
1103 * 0x80 is used as a nonsense port to prevent against
1104 * false positives due to ISA bus float. The
1105 * assumption is that 0x80 is a non-existent port;
1106 * which should be safe since include/asm/io.h also
1107 * makes this assumption.
1109 * Note: this is safe as long as MCR bit 4 is clear
1110 * and the device is in "PC" mode.
1112 scratch = serial_inp(up, UART_IER);
1113 serial_outp(up, UART_IER, 0);
1114 #ifdef __i386__
1115 outb(0xff, 0x080);
1116 #endif
1118 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1119 * 16C754B) allow only to modify them if an EFR bit is set.
1121 scratch2 = serial_inp(up, UART_IER) & 0x0f;
1122 serial_outp(up, UART_IER, 0x0F);
1123 #ifdef __i386__
1124 outb(0, 0x080);
1125 #endif
1126 scratch3 = serial_inp(up, UART_IER) & 0x0f;
1127 serial_outp(up, UART_IER, scratch);
1128 if (scratch2 != 0 || scratch3 != 0x0F) {
1130 * We failed; there's nothing here
1132 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1133 scratch2, scratch3);
1134 goto out;
1138 save_mcr = serial_in(up, UART_MCR);
1139 save_lcr = serial_in(up, UART_LCR);
1142 * Check to see if a UART is really there. Certain broken
1143 * internal modems based on the Rockwell chipset fail this
1144 * test, because they apparently don't implement the loopback
1145 * test mode. So this test is skipped on the COM 1 through
1146 * COM 4 ports. This *should* be safe, since no board
1147 * manufacturer would be stupid enough to design a board
1148 * that conflicts with COM 1-4 --- we hope!
1150 if (!(up->port.flags & UPF_SKIP_TEST)) {
1151 serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A);
1152 status1 = serial_inp(up, UART_MSR) & 0xF0;
1153 serial_outp(up, UART_MCR, save_mcr);
1154 if (status1 != 0x90) {
1155 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1156 status1);
1157 goto out;
1162 * We're pretty sure there's a port here. Lets find out what
1163 * type of port it is. The IIR top two bits allows us to find
1164 * out if it's 8250 or 16450, 16550, 16550A or later. This
1165 * determines what we test for next.
1167 * We also initialise the EFR (if any) to zero for later. The
1168 * EFR occupies the same register location as the FCR and IIR.
1170 serial_outp(up, UART_LCR, 0xBF);
1171 serial_outp(up, UART_EFR, 0);
1172 serial_outp(up, UART_LCR, 0);
1174 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1175 scratch = serial_in(up, UART_IIR) >> 6;
1177 DEBUG_AUTOCONF("iir=%d ", scratch);
1179 switch (scratch) {
1180 case 0:
1181 autoconfig_8250(up);
1182 break;
1183 case 1:
1184 up->port.type = PORT_UNKNOWN;
1185 break;
1186 case 2:
1187 up->port.type = PORT_16550;
1188 break;
1189 case 3:
1190 autoconfig_16550a(up);
1191 break;
1194 #ifdef CONFIG_SERIAL_8250_RSA
1196 * Only probe for RSA ports if we got the region.
1198 if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) {
1199 int i;
1201 for (i = 0 ; i < probe_rsa_count; ++i) {
1202 if (probe_rsa[i] == up->port.iobase &&
1203 __enable_rsa(up)) {
1204 up->port.type = PORT_RSA;
1205 break;
1209 #endif
1211 #ifdef CONFIG_SERIAL_8250_AU1X00
1212 /* if access method is AU, it is a 16550 with a quirk */
1213 if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU)
1214 up->bugs |= UART_BUG_NOMSR;
1215 #endif
1217 serial_outp(up, UART_LCR, save_lcr);
1219 if (up->capabilities != uart_config[up->port.type].flags) {
1220 printk(KERN_WARNING
1221 "ttyS%d: detected caps %08x should be %08x\n",
1222 serial_index(&up->port), up->capabilities,
1223 uart_config[up->port.type].flags);
1226 up->port.fifosize = uart_config[up->port.type].fifo_size;
1227 up->capabilities = uart_config[up->port.type].flags;
1228 up->tx_loadsz = uart_config[up->port.type].tx_loadsz;
1230 if (up->port.type == PORT_UNKNOWN)
1231 goto out;
1234 * Reset the UART.
1236 #ifdef CONFIG_SERIAL_8250_RSA
1237 if (up->port.type == PORT_RSA)
1238 serial_outp(up, UART_RSA_FRR, 0);
1239 #endif
1240 serial_outp(up, UART_MCR, save_mcr);
1241 serial8250_clear_fifos(up);
1242 serial_in(up, UART_RX);
1243 if (up->capabilities & UART_CAP_UUE)
1244 serial_outp(up, UART_IER, UART_IER_UUE);
1245 else
1246 serial_outp(up, UART_IER, 0);
1248 out:
1249 spin_unlock_irqrestore(&up->port.lock, flags);
1250 DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
1253 static void autoconfig_irq(struct uart_8250_port *up)
1255 unsigned char save_mcr, save_ier;
1256 unsigned char save_ICP = 0;
1257 unsigned int ICP = 0;
1258 unsigned long irqs;
1259 int irq;
1261 if (up->port.flags & UPF_FOURPORT) {
1262 ICP = (up->port.iobase & 0xfe0) | 0x1f;
1263 save_ICP = inb_p(ICP);
1264 outb_p(0x80, ICP);
1265 (void) inb_p(ICP);
1268 /* forget possible initially masked and pending IRQ */
1269 probe_irq_off(probe_irq_on());
1270 save_mcr = serial_inp(up, UART_MCR);
1271 save_ier = serial_inp(up, UART_IER);
1272 serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
1274 irqs = probe_irq_on();
1275 serial_outp(up, UART_MCR, 0);
1276 udelay(10);
1277 if (up->port.flags & UPF_FOURPORT) {
1278 serial_outp(up, UART_MCR,
1279 UART_MCR_DTR | UART_MCR_RTS);
1280 } else {
1281 serial_outp(up, UART_MCR,
1282 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1284 serial_outp(up, UART_IER, 0x0f); /* enable all intrs */
1285 (void)serial_inp(up, UART_LSR);
1286 (void)serial_inp(up, UART_RX);
1287 (void)serial_inp(up, UART_IIR);
1288 (void)serial_inp(up, UART_MSR);
1289 serial_outp(up, UART_TX, 0xFF);
1290 udelay(20);
1291 irq = probe_irq_off(irqs);
1293 serial_outp(up, UART_MCR, save_mcr);
1294 serial_outp(up, UART_IER, save_ier);
1296 if (up->port.flags & UPF_FOURPORT)
1297 outb_p(save_ICP, ICP);
1299 up->port.irq = (irq > 0) ? irq : 0;
1302 static inline void __stop_tx(struct uart_8250_port *p)
1304 if (p->ier & UART_IER_THRI) {
1305 p->ier &= ~UART_IER_THRI;
1306 serial_out(p, UART_IER, p->ier);
1310 static void serial8250_stop_tx(struct uart_port *port)
1312 struct uart_8250_port *up = (struct uart_8250_port *)port;
1314 __stop_tx(up);
1317 * We really want to stop the transmitter from sending.
1319 if (up->port.type == PORT_16C950) {
1320 up->acr |= UART_ACR_TXDIS;
1321 serial_icr_write(up, UART_ACR, up->acr);
1325 static void transmit_chars(struct uart_8250_port *up);
1327 static void serial8250_start_tx(struct uart_port *port)
1329 struct uart_8250_port *up = (struct uart_8250_port *)port;
1331 if (!(up->ier & UART_IER_THRI)) {
1332 up->ier |= UART_IER_THRI;
1333 serial_out(up, UART_IER, up->ier);
1335 if (up->bugs & UART_BUG_TXEN) {
1336 unsigned char lsr, iir;
1337 lsr = serial_in(up, UART_LSR);
1338 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1339 iir = serial_in(up, UART_IIR) & 0x0f;
1340 if ((up->port.type == PORT_RM9000) ?
1341 (lsr & UART_LSR_THRE &&
1342 (iir == UART_IIR_NO_INT || iir == UART_IIR_THRI)) :
1343 (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT))
1344 transmit_chars(up);
1349 * Re-enable the transmitter if we disabled it.
1351 if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1352 up->acr &= ~UART_ACR_TXDIS;
1353 serial_icr_write(up, UART_ACR, up->acr);
1357 static void serial8250_stop_rx(struct uart_port *port)
1359 struct uart_8250_port *up = (struct uart_8250_port *)port;
1361 up->ier &= ~UART_IER_RLSI;
1362 up->port.read_status_mask &= ~UART_LSR_DR;
1363 serial_out(up, UART_IER, up->ier);
1366 static void serial8250_enable_ms(struct uart_port *port)
1368 struct uart_8250_port *up = (struct uart_8250_port *)port;
1370 /* no MSR capabilities */
1371 if (up->bugs & UART_BUG_NOMSR)
1372 return;
1374 up->ier |= UART_IER_MSI;
1375 serial_out(up, UART_IER, up->ier);
1378 static void
1379 receive_chars(struct uart_8250_port *up, unsigned int *status)
1381 struct tty_struct *tty = up->port.info->port.tty;
1382 unsigned char ch, lsr = *status;
1383 int max_count = 256;
1384 char flag;
1386 do {
1387 if (likely(lsr & UART_LSR_DR))
1388 ch = serial_inp(up, UART_RX);
1389 else
1391 * Intel 82571 has a Serial Over Lan device that will
1392 * set UART_LSR_BI without setting UART_LSR_DR when
1393 * it receives a break. To avoid reading from the
1394 * receive buffer without UART_LSR_DR bit set, we
1395 * just force the read character to be 0
1397 ch = 0;
1399 flag = TTY_NORMAL;
1400 up->port.icount.rx++;
1402 lsr |= up->lsr_saved_flags;
1403 up->lsr_saved_flags = 0;
1405 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1407 * For statistics only
1409 if (lsr & UART_LSR_BI) {
1410 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1411 up->port.icount.brk++;
1413 * We do the SysRQ and SAK checking
1414 * here because otherwise the break
1415 * may get masked by ignore_status_mask
1416 * or read_status_mask.
1418 if (uart_handle_break(&up->port))
1419 goto ignore_char;
1420 } else if (lsr & UART_LSR_PE)
1421 up->port.icount.parity++;
1422 else if (lsr & UART_LSR_FE)
1423 up->port.icount.frame++;
1424 if (lsr & UART_LSR_OE)
1425 up->port.icount.overrun++;
1428 * Mask off conditions which should be ignored.
1430 lsr &= up->port.read_status_mask;
1432 if (lsr & UART_LSR_BI) {
1433 DEBUG_INTR("handling break....");
1434 flag = TTY_BREAK;
1435 } else if (lsr & UART_LSR_PE)
1436 flag = TTY_PARITY;
1437 else if (lsr & UART_LSR_FE)
1438 flag = TTY_FRAME;
1440 if (uart_handle_sysrq_char(&up->port, ch))
1441 goto ignore_char;
1443 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
1445 ignore_char:
1446 lsr = serial_inp(up, UART_LSR);
1447 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
1448 spin_unlock(&up->port.lock);
1449 tty_flip_buffer_push(tty);
1450 spin_lock(&up->port.lock);
1451 *status = lsr;
1454 static void transmit_chars(struct uart_8250_port *up)
1456 struct circ_buf *xmit = &up->port.info->xmit;
1457 int count;
1459 if (up->port.x_char) {
1460 serial_outp(up, UART_TX, up->port.x_char);
1461 up->port.icount.tx++;
1462 up->port.x_char = 0;
1463 return;
1465 if (uart_tx_stopped(&up->port)) {
1466 serial8250_stop_tx(&up->port);
1467 return;
1469 if (uart_circ_empty(xmit)) {
1470 __stop_tx(up);
1471 return;
1474 count = up->tx_loadsz;
1475 do {
1476 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1477 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1478 up->port.icount.tx++;
1479 if (uart_circ_empty(xmit))
1480 break;
1481 } while (--count > 0);
1483 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1484 uart_write_wakeup(&up->port);
1486 DEBUG_INTR("THRE...");
1488 if (uart_circ_empty(xmit))
1489 __stop_tx(up);
1492 static unsigned int check_modem_status(struct uart_8250_port *up)
1494 unsigned int status = serial_in(up, UART_MSR);
1496 status |= up->msr_saved_flags;
1497 up->msr_saved_flags = 0;
1498 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1499 up->port.info != NULL) {
1500 if (status & UART_MSR_TERI)
1501 up->port.icount.rng++;
1502 if (status & UART_MSR_DDSR)
1503 up->port.icount.dsr++;
1504 if (status & UART_MSR_DDCD)
1505 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
1506 if (status & UART_MSR_DCTS)
1507 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
1509 wake_up_interruptible(&up->port.info->delta_msr_wait);
1512 return status;
1516 * This handles the interrupt from one port.
1518 static void serial8250_handle_port(struct uart_8250_port *up)
1520 unsigned int status;
1521 unsigned long flags;
1523 spin_lock_irqsave(&up->port.lock, flags);
1525 status = serial_inp(up, UART_LSR);
1527 DEBUG_INTR("status = %x...", status);
1529 if (status & (UART_LSR_DR | UART_LSR_BI))
1530 receive_chars(up, &status);
1531 check_modem_status(up);
1532 if (status & UART_LSR_THRE)
1533 transmit_chars(up);
1535 spin_unlock_irqrestore(&up->port.lock, flags);
1539 * This is the serial driver's interrupt routine.
1541 * Arjan thinks the old way was overly complex, so it got simplified.
1542 * Alan disagrees, saying that need the complexity to handle the weird
1543 * nature of ISA shared interrupts. (This is a special exception.)
1545 * In order to handle ISA shared interrupts properly, we need to check
1546 * that all ports have been serviced, and therefore the ISA interrupt
1547 * line has been de-asserted.
1549 * This means we need to loop through all ports. checking that they
1550 * don't have an interrupt pending.
1552 static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
1554 struct irq_info *i = dev_id;
1555 struct list_head *l, *end = NULL;
1556 #ifndef CONFIG_PREEMPT_RT
1557 int pass_counter = 0;
1558 #endif
1559 int handled = 0;
1561 DEBUG_INTR("serial8250_interrupt(%d)...", irq);
1563 spin_lock(&i->lock);
1565 l = i->head;
1566 do {
1567 struct uart_8250_port *up;
1568 unsigned int iir;
1570 up = list_entry(l, struct uart_8250_port, list);
1572 iir = serial_in(up, UART_IIR);
1573 if (!(iir & UART_IIR_NO_INT)) {
1574 serial8250_handle_port(up);
1576 handled = 1;
1578 end = NULL;
1579 } else if (up->port.iotype == UPIO_DWAPB &&
1580 (iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
1581 /* The DesignWare APB UART has an Busy Detect (0x07)
1582 * interrupt meaning an LCR write attempt occured while the
1583 * UART was busy. The interrupt must be cleared by reading
1584 * the UART status register (USR) and the LCR re-written. */
1585 unsigned int status;
1586 status = *(volatile u32 *)up->port.private_data;
1587 serial_out(up, UART_LCR, up->lcr);
1589 handled = 1;
1591 end = NULL;
1592 } else if (end == NULL)
1593 end = l;
1595 l = l->next;
1598 * On preempt-rt we can be preempted and run in our
1599 * own thread.
1601 #ifndef CONFIG_PREEMPT_RT
1602 if (l == i->head && pass_counter++ > PASS_LIMIT) {
1603 /* If we hit this, we're dead. */
1604 printk(KERN_ERR "serial8250: too much work for "
1605 "irq%d\n", irq);
1606 break;
1608 #endif
1609 } while (l != end);
1611 spin_unlock(&i->lock);
1613 DEBUG_INTR("end.\n");
1615 return IRQ_RETVAL(handled);
1619 * To support ISA shared interrupts, we need to have one interrupt
1620 * handler that ensures that the IRQ line has been deasserted
1621 * before returning. Failing to do this will result in the IRQ
1622 * line being stuck active, and, since ISA irqs are edge triggered,
1623 * no more IRQs will be seen.
1625 static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
1627 spin_lock_irq(&i->lock);
1629 if (!list_empty(i->head)) {
1630 if (i->head == &up->list)
1631 i->head = i->head->next;
1632 list_del(&up->list);
1633 } else {
1634 BUG_ON(i->head != &up->list);
1635 i->head = NULL;
1637 spin_unlock_irq(&i->lock);
1638 /* List empty so throw away the hash node */
1639 if (i->head == NULL) {
1640 hlist_del(&i->node);
1641 kfree(i);
1645 static int serial_link_irq_chain(struct uart_8250_port *up)
1647 struct hlist_head *h;
1648 struct hlist_node *n;
1649 struct irq_info *i;
1650 int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0;
1652 mutex_lock(&hash_mutex);
1654 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1656 hlist_for_each(n, h) {
1657 i = hlist_entry(n, struct irq_info, node);
1658 if (i->irq == up->port.irq)
1659 break;
1662 if (n == NULL) {
1663 i = kzalloc(sizeof(struct irq_info), GFP_KERNEL);
1664 if (i == NULL) {
1665 mutex_unlock(&hash_mutex);
1666 return -ENOMEM;
1668 spin_lock_init(&i->lock);
1669 i->irq = up->port.irq;
1670 hlist_add_head(&i->node, h);
1672 mutex_unlock(&hash_mutex);
1674 spin_lock_irq(&i->lock);
1676 if (i->head) {
1677 list_add(&up->list, i->head);
1678 spin_unlock_irq(&i->lock);
1680 ret = 0;
1681 } else {
1682 INIT_LIST_HEAD(&up->list);
1683 i->head = &up->list;
1684 spin_unlock_irq(&i->lock);
1686 ret = request_irq(up->port.irq, serial8250_interrupt,
1687 irq_flags, "serial", i);
1688 if (ret < 0)
1689 serial_do_unlink(i, up);
1692 return ret;
1695 static void serial_unlink_irq_chain(struct uart_8250_port *up)
1697 struct irq_info *i;
1698 struct hlist_node *n;
1699 struct hlist_head *h;
1701 mutex_lock(&hash_mutex);
1703 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
1705 hlist_for_each(n, h) {
1706 i = hlist_entry(n, struct irq_info, node);
1707 if (i->irq == up->port.irq)
1708 break;
1711 BUG_ON(n == NULL);
1712 BUG_ON(i->head == NULL);
1714 if (list_empty(i->head))
1715 free_irq(up->port.irq, i);
1717 serial_do_unlink(i, up);
1718 mutex_unlock(&hash_mutex);
1721 /* Base timer interval for polling */
1722 static inline int poll_timeout(int timeout)
1724 return timeout > 6 ? (timeout / 2 - 2) : 1;
1728 * This function is used to handle ports that do not have an
1729 * interrupt. This doesn't work very well for 16450's, but gives
1730 * barely passable results for a 16550A. (Although at the expense
1731 * of much CPU overhead).
1733 static void serial8250_timeout(unsigned long data)
1735 struct uart_8250_port *up = (struct uart_8250_port *)data;
1736 unsigned int iir;
1738 iir = serial_in(up, UART_IIR);
1739 if (!(iir & UART_IIR_NO_INT))
1740 serial8250_handle_port(up);
1741 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
1744 static void serial8250_backup_timeout(unsigned long data)
1746 struct uart_8250_port *up = (struct uart_8250_port *)data;
1747 unsigned int iir, ier = 0, lsr;
1748 unsigned long flags;
1751 * Must disable interrupts or else we risk racing with the interrupt
1752 * based handler.
1754 if (is_real_interrupt(up->port.irq)) {
1755 ier = serial_in(up, UART_IER);
1756 serial_out(up, UART_IER, 0);
1759 iir = serial_in(up, UART_IIR);
1762 * This should be a safe test for anyone who doesn't trust the
1763 * IIR bits on their UART, but it's specifically designed for
1764 * the "Diva" UART used on the management processor on many HP
1765 * ia64 and parisc boxes.
1767 spin_lock_irqsave(&up->port.lock, flags);
1768 lsr = serial_in(up, UART_LSR);
1769 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1770 spin_unlock_irqrestore(&up->port.lock, flags);
1771 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
1772 (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) &&
1773 (lsr & UART_LSR_THRE)) {
1774 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
1775 iir |= UART_IIR_THRI;
1778 if (!(iir & UART_IIR_NO_INT))
1779 serial8250_handle_port(up);
1781 if (is_real_interrupt(up->port.irq))
1782 serial_out(up, UART_IER, ier);
1784 /* Standard timer interval plus 0.2s to keep the port running */
1785 mod_timer(&up->timer,
1786 jiffies + poll_timeout(up->port.timeout) + HZ / 5);
1789 static unsigned int serial8250_tx_empty(struct uart_port *port)
1791 struct uart_8250_port *up = (struct uart_8250_port *)port;
1792 unsigned long flags;
1793 unsigned int lsr;
1795 spin_lock_irqsave(&up->port.lock, flags);
1796 lsr = serial_in(up, UART_LSR);
1797 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1798 spin_unlock_irqrestore(&up->port.lock, flags);
1800 return lsr & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
1803 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1805 struct uart_8250_port *up = (struct uart_8250_port *)port;
1806 unsigned int status;
1807 unsigned int ret;
1809 status = check_modem_status(up);
1811 ret = 0;
1812 if (status & UART_MSR_DCD)
1813 ret |= TIOCM_CAR;
1814 if (status & UART_MSR_RI)
1815 ret |= TIOCM_RNG;
1816 if (status & UART_MSR_DSR)
1817 ret |= TIOCM_DSR;
1818 if (status & UART_MSR_CTS)
1819 ret |= TIOCM_CTS;
1820 return ret;
1823 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1825 struct uart_8250_port *up = (struct uart_8250_port *)port;
1826 unsigned char mcr = 0;
1828 if (mctrl & TIOCM_RTS)
1829 mcr |= UART_MCR_RTS;
1830 if (mctrl & TIOCM_DTR)
1831 mcr |= UART_MCR_DTR;
1832 if (mctrl & TIOCM_OUT1)
1833 mcr |= UART_MCR_OUT1;
1834 if (mctrl & TIOCM_OUT2)
1835 mcr |= UART_MCR_OUT2;
1836 if (mctrl & TIOCM_LOOP)
1837 mcr |= UART_MCR_LOOP;
1839 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1841 serial_out(up, UART_MCR, mcr);
1844 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1846 struct uart_8250_port *up = (struct uart_8250_port *)port;
1847 unsigned long flags;
1849 spin_lock_irqsave(&up->port.lock, flags);
1850 if (break_state == -1)
1851 up->lcr |= UART_LCR_SBC;
1852 else
1853 up->lcr &= ~UART_LCR_SBC;
1854 serial_out(up, UART_LCR, up->lcr);
1855 spin_unlock_irqrestore(&up->port.lock, flags);
1858 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
1861 * Wait for transmitter & holding register to empty
1863 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
1865 unsigned int status, tmout = 10000;
1867 /* Wait up to 10ms for the character(s) to be sent. */
1868 do {
1869 status = serial_in(up, UART_LSR);
1871 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
1873 if (--tmout == 0)
1874 break;
1875 udelay(1);
1876 } while ((status & bits) != bits);
1878 /* Wait up to 1s for flow control if necessary */
1879 if (up->port.flags & UPF_CONS_FLOW) {
1880 unsigned int tmout;
1881 for (tmout = 1000000; tmout; tmout--) {
1882 unsigned int msr = serial_in(up, UART_MSR);
1883 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
1884 if (msr & UART_MSR_CTS)
1885 break;
1886 udelay(1);
1887 touch_nmi_watchdog();
1892 #ifdef CONFIG_CONSOLE_POLL
1894 * Console polling routines for writing and reading from the uart while
1895 * in an interrupt or debug context.
1898 static int serial8250_get_poll_char(struct uart_port *port)
1900 struct uart_8250_port *up = (struct uart_8250_port *)port;
1901 unsigned char lsr = serial_inp(up, UART_LSR);
1903 while (!(lsr & UART_LSR_DR))
1904 lsr = serial_inp(up, UART_LSR);
1906 return serial_inp(up, UART_RX);
1910 static void serial8250_put_poll_char(struct uart_port *port,
1911 unsigned char c)
1913 unsigned int ier;
1914 struct uart_8250_port *up = (struct uart_8250_port *)port;
1917 * First save the IER then disable the interrupts
1919 ier = serial_in(up, UART_IER);
1920 if (up->capabilities & UART_CAP_UUE)
1921 serial_out(up, UART_IER, UART_IER_UUE);
1922 else
1923 serial_out(up, UART_IER, 0);
1925 wait_for_xmitr(up, BOTH_EMPTY);
1927 * Send the character out.
1928 * If a LF, also do CR...
1930 serial_out(up, UART_TX, c);
1931 if (c == 10) {
1932 wait_for_xmitr(up, BOTH_EMPTY);
1933 serial_out(up, UART_TX, 13);
1937 * Finally, wait for transmitter to become empty
1938 * and restore the IER
1940 wait_for_xmitr(up, BOTH_EMPTY);
1941 serial_out(up, UART_IER, ier);
1944 #endif /* CONFIG_CONSOLE_POLL */
1946 static int serial8250_startup(struct uart_port *port)
1948 struct uart_8250_port *up = (struct uart_8250_port *)port;
1949 unsigned long flags;
1950 unsigned char lsr, iir;
1951 int retval;
1953 up->capabilities = uart_config[up->port.type].flags;
1954 up->mcr = 0;
1956 if (up->port.iotype != up->cur_iotype)
1957 set_io_from_upio(port);
1959 if (up->port.type == PORT_16C950) {
1960 /* Wake up and initialize UART */
1961 up->acr = 0;
1962 serial_outp(up, UART_LCR, 0xBF);
1963 serial_outp(up, UART_EFR, UART_EFR_ECB);
1964 serial_outp(up, UART_IER, 0);
1965 serial_outp(up, UART_LCR, 0);
1966 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
1967 serial_outp(up, UART_LCR, 0xBF);
1968 serial_outp(up, UART_EFR, UART_EFR_ECB);
1969 serial_outp(up, UART_LCR, 0);
1972 #ifdef CONFIG_SERIAL_8250_RSA
1974 * If this is an RSA port, see if we can kick it up to the
1975 * higher speed clock.
1977 enable_rsa(up);
1978 #endif
1981 * Clear the FIFO buffers and disable them.
1982 * (they will be reenabled in set_termios())
1984 serial8250_clear_fifos(up);
1987 * Clear the interrupt registers.
1989 (void) serial_inp(up, UART_LSR);
1990 (void) serial_inp(up, UART_RX);
1991 (void) serial_inp(up, UART_IIR);
1992 (void) serial_inp(up, UART_MSR);
1995 * At this point, there's no way the LSR could still be 0xff;
1996 * if it is, then bail out, because there's likely no UART
1997 * here.
1999 if (!(up->port.flags & UPF_BUGGY_UART) &&
2000 (serial_inp(up, UART_LSR) == 0xff)) {
2001 printk(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2002 serial_index(&up->port));
2003 return -ENODEV;
2007 * For a XR16C850, we need to set the trigger levels
2009 if (up->port.type == PORT_16850) {
2010 unsigned char fctr;
2012 serial_outp(up, UART_LCR, 0xbf);
2014 fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2015 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2016 serial_outp(up, UART_TRG, UART_TRG_96);
2017 serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2018 serial_outp(up, UART_TRG, UART_TRG_96);
2020 serial_outp(up, UART_LCR, 0);
2023 if (is_real_interrupt(up->port.irq)) {
2024 unsigned char iir1;
2026 * Test for UARTs that do not reassert THRE when the
2027 * transmitter is idle and the interrupt has already
2028 * been cleared. Real 16550s should always reassert
2029 * this interrupt whenever the transmitter is idle and
2030 * the interrupt is enabled. Delays are necessary to
2031 * allow register changes to become visible.
2033 spin_lock_irqsave(&up->port.lock, flags);
2034 if (up->port.flags & UPF_SHARE_IRQ)
2035 disable_irq_nosync(up->port.irq);
2037 wait_for_xmitr(up, UART_LSR_THRE);
2038 serial_out_sync(up, UART_IER, UART_IER_THRI);
2039 udelay(1); /* allow THRE to set */
2040 iir1 = serial_in(up, UART_IIR);
2041 serial_out(up, UART_IER, 0);
2042 serial_out_sync(up, UART_IER, UART_IER_THRI);
2043 udelay(1); /* allow a working UART time to re-assert THRE */
2044 iir = serial_in(up, UART_IIR);
2045 serial_out(up, UART_IER, 0);
2047 if (up->port.flags & UPF_SHARE_IRQ)
2048 enable_irq(up->port.irq);
2049 spin_unlock_irqrestore(&up->port.lock, flags);
2052 * If the interrupt is not reasserted, setup a timer to
2053 * kick the UART on a regular basis.
2055 if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) {
2056 up->bugs |= UART_BUG_THRE;
2057 pr_debug("ttyS%d - using backup timer\n",
2058 serial_index(port));
2063 * The above check will only give an accurate result the first time
2064 * the port is opened so this value needs to be preserved.
2066 if (up->bugs & UART_BUG_THRE) {
2067 up->timer.function = serial8250_backup_timeout;
2068 up->timer.data = (unsigned long)up;
2069 mod_timer(&up->timer, jiffies +
2070 poll_timeout(up->port.timeout) + HZ / 5);
2074 * If the "interrupt" for this port doesn't correspond with any
2075 * hardware interrupt, we use a timer-based system. The original
2076 * driver used to do this with IRQ0.
2078 if (!is_real_interrupt(up->port.irq)) {
2079 up->timer.data = (unsigned long)up;
2080 mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout));
2081 } else {
2082 retval = serial_link_irq_chain(up);
2083 if (retval)
2084 return retval;
2088 * Now, initialize the UART
2090 serial_outp(up, UART_LCR, UART_LCR_WLEN8);
2092 spin_lock_irqsave(&up->port.lock, flags);
2093 if (up->port.flags & UPF_FOURPORT) {
2094 if (!is_real_interrupt(up->port.irq))
2095 up->port.mctrl |= TIOCM_OUT1;
2096 } else
2098 * Most PC uarts need OUT2 raised to enable interrupts.
2100 if (is_real_interrupt(up->port.irq))
2101 up->port.mctrl |= TIOCM_OUT2;
2103 serial8250_set_mctrl(&up->port, up->port.mctrl);
2105 /* Serial over Lan (SoL) hack:
2106 Intel 8257x Gigabit ethernet chips have a
2107 16550 emulation, to be used for Serial Over Lan.
2108 Those chips take a longer time than a normal
2109 serial device to signalize that a transmission
2110 data was queued. Due to that, the above test generally
2111 fails. One solution would be to delay the reading of
2112 iir. However, this is not reliable, since the timeout
2113 is variable. So, let's just don't test if we receive
2114 TX irq. This way, we'll never enable UART_BUG_TXEN.
2116 if (up->port.flags & UPF_NO_TXEN_TEST)
2117 goto dont_test_tx_en;
2120 * Do a quick test to see if we receive an
2121 * interrupt when we enable the TX irq.
2123 serial_outp(up, UART_IER, UART_IER_THRI);
2124 lsr = serial_in(up, UART_LSR);
2125 iir = serial_in(up, UART_IIR);
2126 serial_outp(up, UART_IER, 0);
2128 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2129 if (!(up->bugs & UART_BUG_TXEN)) {
2130 up->bugs |= UART_BUG_TXEN;
2131 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2132 serial_index(port));
2134 } else {
2135 up->bugs &= ~UART_BUG_TXEN;
2138 dont_test_tx_en:
2139 spin_unlock_irqrestore(&up->port.lock, flags);
2142 * Clear the interrupt registers again for luck, and clear the
2143 * saved flags to avoid getting false values from polling
2144 * routines or the previous session.
2146 serial_inp(up, UART_LSR);
2147 serial_inp(up, UART_RX);
2148 serial_inp(up, UART_IIR);
2149 serial_inp(up, UART_MSR);
2150 up->lsr_saved_flags = 0;
2151 up->msr_saved_flags = 0;
2154 * Finally, enable interrupts. Note: Modem status interrupts
2155 * are set via set_termios(), which will be occurring imminently
2156 * anyway, so we don't enable them here.
2158 up->ier = UART_IER_RLSI | UART_IER_RDI;
2159 serial_outp(up, UART_IER, up->ier);
2161 if (up->port.flags & UPF_FOURPORT) {
2162 unsigned int icp;
2164 * Enable interrupts on the AST Fourport board
2166 icp = (up->port.iobase & 0xfe0) | 0x01f;
2167 outb_p(0x80, icp);
2168 (void) inb_p(icp);
2171 return 0;
2174 static void serial8250_shutdown(struct uart_port *port)
2176 struct uart_8250_port *up = (struct uart_8250_port *)port;
2177 unsigned long flags;
2180 * Disable interrupts from this port
2182 up->ier = 0;
2183 serial_outp(up, UART_IER, 0);
2185 spin_lock_irqsave(&up->port.lock, flags);
2186 if (up->port.flags & UPF_FOURPORT) {
2187 /* reset interrupts on the AST Fourport board */
2188 inb((up->port.iobase & 0xfe0) | 0x1f);
2189 up->port.mctrl |= TIOCM_OUT1;
2190 } else
2191 up->port.mctrl &= ~TIOCM_OUT2;
2193 serial8250_set_mctrl(&up->port, up->port.mctrl);
2194 spin_unlock_irqrestore(&up->port.lock, flags);
2197 * Disable break condition and FIFOs
2199 serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
2200 serial8250_clear_fifos(up);
2202 #ifdef CONFIG_SERIAL_8250_RSA
2204 * Reset the RSA board back to 115kbps compat mode.
2206 disable_rsa(up);
2207 #endif
2210 * Read data port to reset things, and then unlink from
2211 * the IRQ chain.
2213 (void) serial_in(up, UART_RX);
2215 del_timer_sync(&up->timer);
2216 up->timer.function = serial8250_timeout;
2217 if (is_real_interrupt(up->port.irq))
2218 serial_unlink_irq_chain(up);
2221 static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
2223 unsigned int quot;
2226 * Handle magic divisors for baud rates above baud_base on
2227 * SMSC SuperIO chips.
2229 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2230 baud == (port->uartclk/4))
2231 quot = 0x8001;
2232 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2233 baud == (port->uartclk/8))
2234 quot = 0x8002;
2235 else
2236 quot = uart_get_divisor(port, baud);
2238 return quot;
2241 static void
2242 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2243 struct ktermios *old)
2245 struct uart_8250_port *up = (struct uart_8250_port *)port;
2246 unsigned char cval, fcr = 0;
2247 unsigned long flags;
2248 unsigned int baud, quot;
2250 switch (termios->c_cflag & CSIZE) {
2251 case CS5:
2252 cval = UART_LCR_WLEN5;
2253 break;
2254 case CS6:
2255 cval = UART_LCR_WLEN6;
2256 break;
2257 case CS7:
2258 cval = UART_LCR_WLEN7;
2259 break;
2260 default:
2261 case CS8:
2262 cval = UART_LCR_WLEN8;
2263 break;
2266 if (termios->c_cflag & CSTOPB)
2267 cval |= UART_LCR_STOP;
2268 if (termios->c_cflag & PARENB)
2269 cval |= UART_LCR_PARITY;
2270 if (!(termios->c_cflag & PARODD))
2271 cval |= UART_LCR_EPAR;
2272 #ifdef CMSPAR
2273 if (termios->c_cflag & CMSPAR)
2274 cval |= UART_LCR_SPAR;
2275 #endif
2278 * Ask the core to calculate the divisor for us.
2280 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
2281 quot = serial8250_get_divisor(port, baud);
2284 * Oxford Semi 952 rev B workaround
2286 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2287 quot++;
2289 if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) {
2290 if (baud < 2400)
2291 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
2292 else
2293 fcr = uart_config[up->port.type].fcr;
2297 * MCR-based auto flow control. When AFE is enabled, RTS will be
2298 * deasserted when the receive FIFO contains more characters than
2299 * the trigger, or the MCR RTS bit is cleared. In the case where
2300 * the remote UART is not using CTS auto flow control, we must
2301 * have sufficient FIFO entries for the latency of the remote
2302 * UART to respond. IOW, at least 32 bytes of FIFO.
2304 if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) {
2305 up->mcr &= ~UART_MCR_AFE;
2306 if (termios->c_cflag & CRTSCTS)
2307 up->mcr |= UART_MCR_AFE;
2311 * Ok, we're now changing the port state. Do it with
2312 * interrupts disabled.
2314 spin_lock_irqsave(&up->port.lock, flags);
2317 * Update the per-port timeout.
2319 uart_update_timeout(port, termios->c_cflag, baud);
2321 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2322 if (termios->c_iflag & INPCK)
2323 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2324 if (termios->c_iflag & (BRKINT | PARMRK))
2325 up->port.read_status_mask |= UART_LSR_BI;
2328 * Characteres to ignore
2330 up->port.ignore_status_mask = 0;
2331 if (termios->c_iflag & IGNPAR)
2332 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2333 if (termios->c_iflag & IGNBRK) {
2334 up->port.ignore_status_mask |= UART_LSR_BI;
2336 * If we're ignoring parity and break indicators,
2337 * ignore overruns too (for real raw support).
2339 if (termios->c_iflag & IGNPAR)
2340 up->port.ignore_status_mask |= UART_LSR_OE;
2344 * ignore all characters if CREAD is not set
2346 if ((termios->c_cflag & CREAD) == 0)
2347 up->port.ignore_status_mask |= UART_LSR_DR;
2350 * CTS flow control flag and modem status interrupts
2352 up->ier &= ~UART_IER_MSI;
2353 if (!(up->bugs & UART_BUG_NOMSR) &&
2354 UART_ENABLE_MS(&up->port, termios->c_cflag))
2355 up->ier |= UART_IER_MSI;
2356 if (up->capabilities & UART_CAP_UUE)
2357 up->ier |= UART_IER_UUE | UART_IER_RTOIE;
2359 serial_out(up, UART_IER, up->ier);
2361 if (up->capabilities & UART_CAP_EFR) {
2362 unsigned char efr = 0;
2364 * TI16C752/Startech hardware flow control. FIXME:
2365 * - TI16C752 requires control thresholds to be set.
2366 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2368 if (termios->c_cflag & CRTSCTS)
2369 efr |= UART_EFR_CTS;
2371 serial_outp(up, UART_LCR, 0xBF);
2372 serial_outp(up, UART_EFR, efr);
2375 #ifdef CONFIG_ARCH_OMAP
2376 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2377 if (cpu_is_omap1510() && is_omap_port(up)) {
2378 if (baud == 115200) {
2379 quot = 1;
2380 serial_out(up, UART_OMAP_OSC_12M_SEL, 1);
2381 } else
2382 serial_out(up, UART_OMAP_OSC_12M_SEL, 0);
2384 #endif
2386 if (up->capabilities & UART_NATSEMI) {
2387 /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */
2388 serial_outp(up, UART_LCR, 0xe0);
2389 } else {
2390 serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */
2393 serial_dl_write(up, quot);
2396 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2397 * is written without DLAB set, this mode will be disabled.
2399 if (up->port.type == PORT_16750)
2400 serial_outp(up, UART_FCR, fcr);
2402 serial_outp(up, UART_LCR, cval); /* reset DLAB */
2403 up->lcr = cval; /* Save LCR */
2404 if (up->port.type != PORT_16750) {
2405 if (fcr & UART_FCR_ENABLE_FIFO) {
2406 /* emulated UARTs (Lucent Venus 167x) need two steps */
2407 serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
2409 serial_outp(up, UART_FCR, fcr); /* set fcr */
2411 serial8250_set_mctrl(&up->port, up->port.mctrl);
2412 spin_unlock_irqrestore(&up->port.lock, flags);
2413 /* Don't rewrite B0 */
2414 if (tty_termios_baud_rate(termios))
2415 tty_termios_encode_baud_rate(termios, baud, baud);
2418 static void
2419 serial8250_pm(struct uart_port *port, unsigned int state,
2420 unsigned int oldstate)
2422 struct uart_8250_port *p = (struct uart_8250_port *)port;
2424 serial8250_set_sleep(p, state != 0);
2426 if (p->pm)
2427 p->pm(port, state, oldstate);
2430 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2432 if (pt->port.iotype == UPIO_AU)
2433 return 0x100000;
2434 #ifdef CONFIG_ARCH_OMAP
2435 if (is_omap_port(pt))
2436 return 0x16 << pt->port.regshift;
2437 #endif
2438 return 8 << pt->port.regshift;
2442 * Resource handling.
2444 static int serial8250_request_std_resource(struct uart_8250_port *up)
2446 unsigned int size = serial8250_port_size(up);
2447 int ret = 0;
2449 switch (up->port.iotype) {
2450 case UPIO_AU:
2451 case UPIO_TSI:
2452 case UPIO_MEM32:
2453 case UPIO_MEM:
2454 case UPIO_DWAPB:
2455 if (!up->port.mapbase)
2456 break;
2458 if (!request_mem_region(up->port.mapbase, size, "serial")) {
2459 ret = -EBUSY;
2460 break;
2463 if (up->port.flags & UPF_IOREMAP) {
2464 up->port.membase = ioremap_nocache(up->port.mapbase,
2465 size);
2466 if (!up->port.membase) {
2467 release_mem_region(up->port.mapbase, size);
2468 ret = -ENOMEM;
2471 break;
2473 case UPIO_HUB6:
2474 case UPIO_PORT:
2475 if (!request_region(up->port.iobase, size, "serial"))
2476 ret = -EBUSY;
2477 break;
2479 return ret;
2482 static void serial8250_release_std_resource(struct uart_8250_port *up)
2484 unsigned int size = serial8250_port_size(up);
2486 switch (up->port.iotype) {
2487 case UPIO_AU:
2488 case UPIO_TSI:
2489 case UPIO_MEM32:
2490 case UPIO_MEM:
2491 case UPIO_DWAPB:
2492 if (!up->port.mapbase)
2493 break;
2495 if (up->port.flags & UPF_IOREMAP) {
2496 iounmap(up->port.membase);
2497 up->port.membase = NULL;
2500 release_mem_region(up->port.mapbase, size);
2501 break;
2503 case UPIO_HUB6:
2504 case UPIO_PORT:
2505 release_region(up->port.iobase, size);
2506 break;
2510 static int serial8250_request_rsa_resource(struct uart_8250_port *up)
2512 unsigned long start = UART_RSA_BASE << up->port.regshift;
2513 unsigned int size = 8 << up->port.regshift;
2514 int ret = -EINVAL;
2516 switch (up->port.iotype) {
2517 case UPIO_HUB6:
2518 case UPIO_PORT:
2519 start += up->port.iobase;
2520 if (request_region(start, size, "serial-rsa"))
2521 ret = 0;
2522 else
2523 ret = -EBUSY;
2524 break;
2527 return ret;
2530 static void serial8250_release_rsa_resource(struct uart_8250_port *up)
2532 unsigned long offset = UART_RSA_BASE << up->port.regshift;
2533 unsigned int size = 8 << up->port.regshift;
2535 switch (up->port.iotype) {
2536 case UPIO_HUB6:
2537 case UPIO_PORT:
2538 release_region(up->port.iobase + offset, size);
2539 break;
2543 static void serial8250_release_port(struct uart_port *port)
2545 struct uart_8250_port *up = (struct uart_8250_port *)port;
2547 serial8250_release_std_resource(up);
2548 if (up->port.type == PORT_RSA)
2549 serial8250_release_rsa_resource(up);
2552 static int serial8250_request_port(struct uart_port *port)
2554 struct uart_8250_port *up = (struct uart_8250_port *)port;
2555 int ret = 0;
2557 ret = serial8250_request_std_resource(up);
2558 if (ret == 0 && up->port.type == PORT_RSA) {
2559 ret = serial8250_request_rsa_resource(up);
2560 if (ret < 0)
2561 serial8250_release_std_resource(up);
2564 return ret;
2567 static void serial8250_config_port(struct uart_port *port, int flags)
2569 struct uart_8250_port *up = (struct uart_8250_port *)port;
2570 int probeflags = PROBE_ANY;
2571 int ret;
2574 * Find the region that we can probe for. This in turn
2575 * tells us whether we can probe for the type of port.
2577 ret = serial8250_request_std_resource(up);
2578 if (ret < 0)
2579 return;
2581 ret = serial8250_request_rsa_resource(up);
2582 if (ret < 0)
2583 probeflags &= ~PROBE_RSA;
2585 if (up->port.iotype != up->cur_iotype)
2586 set_io_from_upio(port);
2588 if (flags & UART_CONFIG_TYPE)
2589 autoconfig(up, probeflags);
2590 if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
2591 autoconfig_irq(up);
2593 if (up->port.type != PORT_RSA && probeflags & PROBE_RSA)
2594 serial8250_release_rsa_resource(up);
2595 if (up->port.type == PORT_UNKNOWN)
2596 serial8250_release_std_resource(up);
2599 static int
2600 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
2602 if (ser->irq >= nr_irqs || ser->irq < 0 ||
2603 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
2604 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
2605 ser->type == PORT_STARTECH)
2606 return -EINVAL;
2607 return 0;
2610 static const char *
2611 serial8250_type(struct uart_port *port)
2613 int type = port->type;
2615 if (type >= ARRAY_SIZE(uart_config))
2616 type = 0;
2617 return uart_config[type].name;
2620 static struct uart_ops serial8250_pops = {
2621 .tx_empty = serial8250_tx_empty,
2622 .set_mctrl = serial8250_set_mctrl,
2623 .get_mctrl = serial8250_get_mctrl,
2624 .stop_tx = serial8250_stop_tx,
2625 .start_tx = serial8250_start_tx,
2626 .stop_rx = serial8250_stop_rx,
2627 .enable_ms = serial8250_enable_ms,
2628 .break_ctl = serial8250_break_ctl,
2629 .startup = serial8250_startup,
2630 .shutdown = serial8250_shutdown,
2631 .set_termios = serial8250_set_termios,
2632 .pm = serial8250_pm,
2633 .type = serial8250_type,
2634 .release_port = serial8250_release_port,
2635 .request_port = serial8250_request_port,
2636 .config_port = serial8250_config_port,
2637 .verify_port = serial8250_verify_port,
2638 #ifdef CONFIG_CONSOLE_POLL
2639 .poll_get_char = serial8250_get_poll_char,
2640 .poll_put_char = serial8250_put_poll_char,
2641 #endif
2644 static struct uart_8250_port serial8250_ports[UART_NR];
2646 static void __init serial8250_isa_init_ports(void)
2648 struct uart_8250_port *up;
2649 static int first = 1;
2650 int i;
2652 if (!first)
2653 return;
2654 first = 0;
2656 for (i = 0; i < nr_uarts; i++) {
2657 struct uart_8250_port *up = &serial8250_ports[i];
2659 up->port.line = i;
2660 spin_lock_init(&up->port.lock);
2662 init_timer(&up->timer);
2663 up->timer.function = serial8250_timeout;
2666 * ALPHA_KLUDGE_MCR needs to be killed.
2668 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
2669 up->mcr_force = ALPHA_KLUDGE_MCR;
2671 up->port.ops = &serial8250_pops;
2674 for (i = 0, up = serial8250_ports;
2675 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
2676 i++, up++) {
2677 up->port.iobase = old_serial_port[i].port;
2678 up->port.irq = irq_canonicalize(old_serial_port[i].irq);
2679 up->port.uartclk = old_serial_port[i].baud_base * 16;
2680 up->port.flags = old_serial_port[i].flags;
2681 up->port.hub6 = old_serial_port[i].hub6;
2682 up->port.membase = old_serial_port[i].iomem_base;
2683 up->port.iotype = old_serial_port[i].io_type;
2684 up->port.regshift = old_serial_port[i].iomem_reg_shift;
2685 set_io_from_upio(&up->port);
2686 if (share_irqs)
2687 up->port.flags |= UPF_SHARE_IRQ;
2691 static void __init
2692 serial8250_register_ports(struct uart_driver *drv, struct device *dev)
2694 int i;
2696 for (i = 0; i < nr_uarts; i++) {
2697 struct uart_8250_port *up = &serial8250_ports[i];
2698 up->cur_iotype = 0xFF;
2701 serial8250_isa_init_ports();
2703 for (i = 0; i < nr_uarts; i++) {
2704 struct uart_8250_port *up = &serial8250_ports[i];
2706 up->port.dev = dev;
2707 uart_add_one_port(drv, &up->port);
2711 #ifdef CONFIG_SERIAL_8250_CONSOLE
2713 static void serial8250_console_putchar(struct uart_port *port, int ch)
2715 struct uart_8250_port *up = (struct uart_8250_port *)port;
2717 wait_for_xmitr(up, UART_LSR_THRE);
2718 serial_out(up, UART_TX, ch);
2722 * Print a string to the serial port trying not to disturb
2723 * any possible real use of the port...
2725 * The console_lock must be held when we get here.
2727 static void
2728 serial8250_console_write(struct console *co, const char *s, unsigned int count)
2730 struct uart_8250_port *up = &serial8250_ports[co->index];
2731 unsigned long flags;
2732 unsigned int ier;
2733 int locked = 1;
2735 touch_nmi_watchdog();
2737 if (up->port.sysrq || oops_in_progress || preempt_rt)
2738 locked = spin_trylock_irqsave(&up->port.lock, flags);
2739 else
2740 spin_lock_irqsave(&up->port.lock, flags);
2743 * First save the IER then disable the interrupts
2745 ier = serial_in(up, UART_IER);
2747 if (up->capabilities & UART_CAP_UUE)
2748 serial_out(up, UART_IER, UART_IER_UUE);
2749 else
2750 serial_out(up, UART_IER, 0);
2752 uart_console_write(&up->port, s, count, serial8250_console_putchar);
2755 * Finally, wait for transmitter to become empty
2756 * and restore the IER
2758 wait_for_xmitr(up, BOTH_EMPTY);
2759 serial_out(up, UART_IER, ier);
2762 * The receive handling will happen properly because the
2763 * receive ready bit will still be set; it is not cleared
2764 * on read. However, modem control will not, we must
2765 * call it if we have saved something in the saved flags
2766 * while processing with interrupts off.
2768 if (up->msr_saved_flags)
2769 check_modem_status(up);
2771 if (locked)
2772 spin_unlock_irqrestore(&up->port.lock, flags);
2775 static int __init serial8250_console_setup(struct console *co, char *options)
2777 struct uart_port *port;
2778 int baud = 9600;
2779 int bits = 8;
2780 int parity = 'n';
2781 int flow = 'n';
2784 * Check whether an invalid uart number has been specified, and
2785 * if so, search for the first available port that does have
2786 * console support.
2788 if (co->index >= nr_uarts)
2789 co->index = 0;
2790 port = &serial8250_ports[co->index].port;
2791 if (!port->iobase && !port->membase)
2792 return -ENODEV;
2794 if (options)
2795 uart_parse_options(options, &baud, &parity, &bits, &flow);
2797 return uart_set_options(port, co, baud, parity, bits, flow);
2800 static int serial8250_console_early_setup(void)
2802 return serial8250_find_port_for_earlycon();
2805 static struct console serial8250_console = {
2806 .name = "ttyS",
2807 .write = serial8250_console_write,
2808 .device = uart_console_device,
2809 .setup = serial8250_console_setup,
2810 .early_setup = serial8250_console_early_setup,
2811 .flags = CON_PRINTBUFFER,
2812 .index = -1,
2813 .data = &serial8250_reg,
2816 static int __init serial8250_console_init(void)
2818 if (nr_uarts > UART_NR)
2819 nr_uarts = UART_NR;
2821 serial8250_isa_init_ports();
2822 register_console(&serial8250_console);
2823 return 0;
2825 console_initcall(serial8250_console_init);
2827 int serial8250_find_port(struct uart_port *p)
2829 int line;
2830 struct uart_port *port;
2832 for (line = 0; line < nr_uarts; line++) {
2833 port = &serial8250_ports[line].port;
2834 if (uart_match_port(p, port))
2835 return line;
2837 return -ENODEV;
2840 #define SERIAL8250_CONSOLE &serial8250_console
2841 #else
2842 #define SERIAL8250_CONSOLE NULL
2843 #endif
2845 static struct uart_driver serial8250_reg = {
2846 .owner = THIS_MODULE,
2847 .driver_name = "serial",
2848 .dev_name = "ttyS",
2849 .major = TTY_MAJOR,
2850 .minor = 64,
2851 .cons = SERIAL8250_CONSOLE,
2855 * early_serial_setup - early registration for 8250 ports
2857 * Setup an 8250 port structure prior to console initialisation. Use
2858 * after console initialisation will cause undefined behaviour.
2860 int __init early_serial_setup(struct uart_port *port)
2862 struct uart_port *p;
2864 if (port->line >= ARRAY_SIZE(serial8250_ports))
2865 return -ENODEV;
2867 serial8250_isa_init_ports();
2868 p = &serial8250_ports[port->line].port;
2869 p->iobase = port->iobase;
2870 p->membase = port->membase;
2871 p->irq = port->irq;
2872 p->uartclk = port->uartclk;
2873 p->fifosize = port->fifosize;
2874 p->regshift = port->regshift;
2875 p->iotype = port->iotype;
2876 p->flags = port->flags;
2877 p->mapbase = port->mapbase;
2878 p->private_data = port->private_data;
2879 p->type = port->type;
2880 p->line = port->line;
2882 set_io_from_upio(p);
2883 if (port->serial_in)
2884 p->serial_in = port->serial_in;
2885 if (port->serial_out)
2886 p->serial_out = port->serial_out;
2888 return 0;
2892 * serial8250_suspend_port - suspend one serial port
2893 * @line: serial line number
2895 * Suspend one serial port.
2897 void serial8250_suspend_port(int line)
2899 uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
2903 * serial8250_resume_port - resume one serial port
2904 * @line: serial line number
2906 * Resume one serial port.
2908 void serial8250_resume_port(int line)
2910 struct uart_8250_port *up = &serial8250_ports[line];
2912 if (up->capabilities & UART_NATSEMI) {
2913 unsigned char tmp;
2915 /* Ensure it's still in high speed mode */
2916 serial_outp(up, UART_LCR, 0xE0);
2918 tmp = serial_in(up, 0x04); /* EXCR2 */
2919 tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
2920 tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
2921 serial_outp(up, 0x04, tmp);
2923 serial_outp(up, UART_LCR, 0);
2925 uart_resume_port(&serial8250_reg, &up->port);
2929 * Register a set of serial devices attached to a platform device. The
2930 * list is terminated with a zero flags entry, which means we expect
2931 * all entries to have at least UPF_BOOT_AUTOCONF set.
2933 static int __devinit serial8250_probe(struct platform_device *dev)
2935 struct plat_serial8250_port *p = dev->dev.platform_data;
2936 struct uart_port port;
2937 int ret, i;
2939 memset(&port, 0, sizeof(struct uart_port));
2941 for (i = 0; p && p->flags != 0; p++, i++) {
2942 port.iobase = p->iobase;
2943 port.membase = p->membase;
2944 port.irq = p->irq;
2945 port.uartclk = p->uartclk;
2946 port.regshift = p->regshift;
2947 port.iotype = p->iotype;
2948 port.flags = p->flags;
2949 port.mapbase = p->mapbase;
2950 port.hub6 = p->hub6;
2951 port.private_data = p->private_data;
2952 port.type = p->type;
2953 port.serial_in = p->serial_in;
2954 port.serial_out = p->serial_out;
2955 port.dev = &dev->dev;
2956 if (share_irqs)
2957 port.flags |= UPF_SHARE_IRQ;
2958 ret = serial8250_register_port(&port);
2959 if (ret < 0) {
2960 dev_err(&dev->dev, "unable to register port at index %d "
2961 "(IO%lx MEM%llx IRQ%d): %d\n", i,
2962 p->iobase, (unsigned long long)p->mapbase,
2963 p->irq, ret);
2966 return 0;
2970 * Remove serial ports registered against a platform device.
2972 static int __devexit serial8250_remove(struct platform_device *dev)
2974 int i;
2976 for (i = 0; i < nr_uarts; i++) {
2977 struct uart_8250_port *up = &serial8250_ports[i];
2979 if (up->port.dev == &dev->dev)
2980 serial8250_unregister_port(i);
2982 return 0;
2985 static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
2987 int i;
2989 for (i = 0; i < UART_NR; i++) {
2990 struct uart_8250_port *up = &serial8250_ports[i];
2992 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
2993 uart_suspend_port(&serial8250_reg, &up->port);
2996 return 0;
2999 static int serial8250_resume(struct platform_device *dev)
3001 int i;
3003 for (i = 0; i < UART_NR; i++) {
3004 struct uart_8250_port *up = &serial8250_ports[i];
3006 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
3007 serial8250_resume_port(i);
3010 return 0;
3013 static struct platform_driver serial8250_isa_driver = {
3014 .probe = serial8250_probe,
3015 .remove = __devexit_p(serial8250_remove),
3016 .suspend = serial8250_suspend,
3017 .resume = serial8250_resume,
3018 .driver = {
3019 .name = "serial8250",
3020 .owner = THIS_MODULE,
3025 * This "device" covers _all_ ISA 8250-compatible serial devices listed
3026 * in the table in include/asm/serial.h
3028 static struct platform_device *serial8250_isa_devs;
3031 * serial8250_register_port and serial8250_unregister_port allows for
3032 * 16x50 serial ports to be configured at run-time, to support PCMCIA
3033 * modems and PCI multiport cards.
3035 static DEFINE_MUTEX(serial_mutex);
3037 static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
3039 int i;
3042 * First, find a port entry which matches.
3044 for (i = 0; i < nr_uarts; i++)
3045 if (uart_match_port(&serial8250_ports[i].port, port))
3046 return &serial8250_ports[i];
3049 * We didn't find a matching entry, so look for the first
3050 * free entry. We look for one which hasn't been previously
3051 * used (indicated by zero iobase).
3053 for (i = 0; i < nr_uarts; i++)
3054 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
3055 serial8250_ports[i].port.iobase == 0)
3056 return &serial8250_ports[i];
3059 * That also failed. Last resort is to find any entry which
3060 * doesn't have a real port associated with it.
3062 for (i = 0; i < nr_uarts; i++)
3063 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
3064 return &serial8250_ports[i];
3066 return NULL;
3070 * serial8250_register_port - register a serial port
3071 * @port: serial port template
3073 * Configure the serial port specified by the request. If the
3074 * port exists and is in use, it is hung up and unregistered
3075 * first.
3077 * The port is then probed and if necessary the IRQ is autodetected
3078 * If this fails an error is returned.
3080 * On success the port is ready to use and the line number is returned.
3082 int serial8250_register_port(struct uart_port *port)
3084 struct uart_8250_port *uart;
3085 int ret = -ENOSPC;
3087 if (port->uartclk == 0)
3088 return -EINVAL;
3090 mutex_lock(&serial_mutex);
3092 uart = serial8250_find_match_or_unused(port);
3093 if (uart) {
3094 uart_remove_one_port(&serial8250_reg, &uart->port);
3096 uart->port.iobase = port->iobase;
3097 uart->port.membase = port->membase;
3098 uart->port.irq = port->irq;
3099 uart->port.uartclk = port->uartclk;
3100 uart->port.fifosize = port->fifosize;
3101 uart->port.regshift = port->regshift;
3102 uart->port.iotype = port->iotype;
3103 uart->port.flags = port->flags | UPF_BOOT_AUTOCONF;
3104 uart->port.mapbase = port->mapbase;
3105 uart->port.private_data = port->private_data;
3106 if (port->dev)
3107 uart->port.dev = port->dev;
3109 if (port->flags & UPF_FIXED_TYPE) {
3110 uart->port.type = port->type;
3111 uart->port.fifosize = uart_config[port->type].fifo_size;
3112 uart->capabilities = uart_config[port->type].flags;
3113 uart->tx_loadsz = uart_config[port->type].tx_loadsz;
3116 set_io_from_upio(&uart->port);
3117 /* Possibly override default I/O functions. */
3118 if (port->serial_in)
3119 uart->port.serial_in = port->serial_in;
3120 if (port->serial_out)
3121 uart->port.serial_out = port->serial_out;
3123 ret = uart_add_one_port(&serial8250_reg, &uart->port);
3124 if (ret == 0)
3125 ret = uart->port.line;
3127 mutex_unlock(&serial_mutex);
3129 return ret;
3131 EXPORT_SYMBOL(serial8250_register_port);
3134 * serial8250_unregister_port - remove a 16x50 serial port at runtime
3135 * @line: serial line number
3137 * Remove one serial port. This may not be called from interrupt
3138 * context. We hand the port back to the our control.
3140 void serial8250_unregister_port(int line)
3142 struct uart_8250_port *uart = &serial8250_ports[line];
3144 mutex_lock(&serial_mutex);
3145 uart_remove_one_port(&serial8250_reg, &uart->port);
3146 if (serial8250_isa_devs) {
3147 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
3148 uart->port.type = PORT_UNKNOWN;
3149 uart->port.dev = &serial8250_isa_devs->dev;
3150 uart_add_one_port(&serial8250_reg, &uart->port);
3151 } else {
3152 uart->port.dev = NULL;
3154 mutex_unlock(&serial_mutex);
3156 EXPORT_SYMBOL(serial8250_unregister_port);
3158 static int __init serial8250_init(void)
3160 int ret;
3162 if (nr_uarts > UART_NR)
3163 nr_uarts = UART_NR;
3165 printk(KERN_INFO "Serial: 8250/16550 driver, "
3166 "%d ports, IRQ sharing %sabled\n", nr_uarts,
3167 share_irqs ? "en" : "dis");
3169 #ifdef CONFIG_SPARC
3170 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
3171 #else
3172 serial8250_reg.nr = UART_NR;
3173 ret = uart_register_driver(&serial8250_reg);
3174 #endif
3175 if (ret)
3176 goto out;
3178 serial8250_isa_devs = platform_device_alloc("serial8250",
3179 PLAT8250_DEV_LEGACY);
3180 if (!serial8250_isa_devs) {
3181 ret = -ENOMEM;
3182 goto unreg_uart_drv;
3185 ret = platform_device_add(serial8250_isa_devs);
3186 if (ret)
3187 goto put_dev;
3189 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
3191 ret = platform_driver_register(&serial8250_isa_driver);
3192 if (ret == 0)
3193 goto out;
3195 platform_device_del(serial8250_isa_devs);
3196 put_dev:
3197 platform_device_put(serial8250_isa_devs);
3198 unreg_uart_drv:
3199 #ifdef CONFIG_SPARC
3200 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3201 #else
3202 uart_unregister_driver(&serial8250_reg);
3203 #endif
3204 out:
3205 return ret;
3208 static void __exit serial8250_exit(void)
3210 struct platform_device *isa_dev = serial8250_isa_devs;
3213 * This tells serial8250_unregister_port() not to re-register
3214 * the ports (thereby making serial8250_isa_driver permanently
3215 * in use.)
3217 serial8250_isa_devs = NULL;
3219 platform_driver_unregister(&serial8250_isa_driver);
3220 platform_device_unregister(isa_dev);
3222 #ifdef CONFIG_SPARC
3223 sunserial_unregister_minors(&serial8250_reg, UART_NR);
3224 #else
3225 uart_unregister_driver(&serial8250_reg);
3226 #endif
3229 module_init(serial8250_init);
3230 module_exit(serial8250_exit);
3232 EXPORT_SYMBOL(serial8250_suspend_port);
3233 EXPORT_SYMBOL(serial8250_resume_port);
3235 MODULE_LICENSE("GPL");
3236 MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
3238 module_param(share_irqs, uint, 0644);
3239 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
3240 " (unsafe)");
3242 module_param(nr_uarts, uint, 0644);
3243 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
3245 #ifdef CONFIG_SERIAL_8250_RSA
3246 module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
3247 MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
3248 #endif
3249 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);