2 * Base port operations for 8250/16550-type serial ports
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 * Split from 8250_core.c, Copyright (C) 2001 Russell King.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * A note about mapbase / membase
14 * mapbase is the physical address of the IO port.
15 * membase is an 'ioremapped' cookie.
18 #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/ioport.h>
25 #include <linux/init.h>
26 #include <linux/console.h>
27 #include <linux/sysrq.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <linux/tty.h>
31 #include <linux/ratelimit.h>
32 #include <linux/tty_flip.h>
33 #include <linux/serial.h>
34 #include <linux/serial_8250.h>
35 #include <linux/nmi.h>
36 #include <linux/mutex.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/timer.h>
51 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
53 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
56 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
59 * Here we define the default xmit fifo size used for each type of UART.
61 static const struct serial8250_config uart_config[] = {
86 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
87 .rxtrig_bytes = {1, 4, 8, 14},
88 .flags = UART_CAP_FIFO,
99 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
105 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
107 .rxtrig_bytes = {8, 16, 24, 28},
108 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
114 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
116 .rxtrig_bytes = {1, 16, 32, 56},
117 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
125 .name = "16C950/954",
128 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
129 .rxtrig_bytes = {16, 32, 112, 120},
130 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
131 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
137 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
139 .rxtrig_bytes = {8, 16, 56, 60},
140 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
146 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
147 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
153 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
154 .flags = UART_CAP_FIFO,
160 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
161 .flags = UART_CAP_FIFO | UART_NATSEMI,
167 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
168 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
174 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
175 .flags = UART_CAP_FIFO,
181 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
182 .flags = UART_CAP_FIFO /* | UART_CAP_AFE */,
188 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
189 .flags = UART_CAP_FIFO | UART_CAP_AFE,
195 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
197 .rxtrig_bytes = {1, 4, 8, 14},
198 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
204 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
205 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
212 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
214 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
221 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
222 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
223 .flags = UART_CAP_FIFO,
225 [PORT_BRCM_TRUMANAGE] = {
229 .flags = UART_CAP_HFIFO,
234 [PORT_ALTR_16550_F32] = {
235 .name = "Altera 16550 FIFO32",
238 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
239 .flags = UART_CAP_FIFO | UART_CAP_AFE,
241 [PORT_ALTR_16550_F64] = {
242 .name = "Altera 16550 FIFO64",
245 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
246 .flags = UART_CAP_FIFO | UART_CAP_AFE,
248 [PORT_ALTR_16550_F128] = {
249 .name = "Altera 16550 FIFO128",
252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
253 .flags = UART_CAP_FIFO | UART_CAP_AFE,
256 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement
257 * workaround of errata A-008006 which states that tx_loadsz should
258 * be configured less than Maximum supported fifo bytes.
260 [PORT_16550A_FSL64] = {
261 .name = "16550A_FSL64",
264 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
266 .flags = UART_CAP_FIFO,
269 .name = "Palmchip BK-3103",
272 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
273 .rxtrig_bytes = {1, 4, 8, 14},
274 .flags = UART_CAP_FIFO,
278 /* Uart divisor latch read */
279 static int default_serial_dl_read(struct uart_8250_port *up)
281 /* Assign these in pieces to truncate any bits above 7. */
282 unsigned char dll = serial_in(up, UART_DLL);
283 unsigned char dlm = serial_in(up, UART_DLM);
285 return dll | dlm << 8;
288 /* Uart divisor latch write */
289 static void default_serial_dl_write(struct uart_8250_port *up, int value)
291 serial_out(up, UART_DLL, value & 0xff);
292 serial_out(up, UART_DLM, value >> 8 & 0xff);
295 #ifdef CONFIG_SERIAL_8250_RT288X
297 /* Au1x00/RT288x UART hardware has a weird register layout */
298 static const s8 au_io_in_map[8] = {
306 -1, /* UART_SCR (unmapped) */
309 static const s8 au_io_out_map[8] = {
315 -1, /* UART_LSR (unmapped) */
316 -1, /* UART_MSR (unmapped) */
317 -1, /* UART_SCR (unmapped) */
320 static unsigned int au_serial_in(struct uart_port *p, int offset)
322 if (offset >= ARRAY_SIZE(au_io_in_map))
324 offset = au_io_in_map[offset];
327 return __raw_readl(p->membase + (offset << p->regshift));
330 static void au_serial_out(struct uart_port *p, int offset, int value)
332 if (offset >= ARRAY_SIZE(au_io_out_map))
334 offset = au_io_out_map[offset];
337 __raw_writel(value, p->membase + (offset << p->regshift));
340 /* Au1x00 haven't got a standard divisor latch */
341 static int au_serial_dl_read(struct uart_8250_port *up)
343 return __raw_readl(up->port.membase + 0x28);
346 static void au_serial_dl_write(struct uart_8250_port *up, int value)
348 __raw_writel(value, up->port.membase + 0x28);
353 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
355 offset = offset << p->regshift;
356 outb(p->hub6 - 1 + offset, p->iobase);
357 return inb(p->iobase + 1);
360 static void hub6_serial_out(struct uart_port *p, int offset, int value)
362 offset = offset << p->regshift;
363 outb(p->hub6 - 1 + offset, p->iobase);
364 outb(value, p->iobase + 1);
367 static unsigned int mem_serial_in(struct uart_port *p, int offset)
369 offset = offset << p->regshift;
370 return readb(p->membase + offset);
373 static void mem_serial_out(struct uart_port *p, int offset, int value)
375 offset = offset << p->regshift;
376 writeb(value, p->membase + offset);
379 static void mem16_serial_out(struct uart_port *p, int offset, int value)
381 offset = offset << p->regshift;
382 writew(value, p->membase + offset);
385 static unsigned int mem16_serial_in(struct uart_port *p, int offset)
387 offset = offset << p->regshift;
388 return readw(p->membase + offset);
391 static void mem32_serial_out(struct uart_port *p, int offset, int value)
393 offset = offset << p->regshift;
394 writel(value, p->membase + offset);
397 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
399 offset = offset << p->regshift;
400 return readl(p->membase + offset);
403 static void mem32be_serial_out(struct uart_port *p, int offset, int value)
405 offset = offset << p->regshift;
406 iowrite32be(value, p->membase + offset);
409 static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
411 offset = offset << p->regshift;
412 return ioread32be(p->membase + offset);
415 static unsigned int io_serial_in(struct uart_port *p, int offset)
417 offset = offset << p->regshift;
418 return inb(p->iobase + offset);
421 static void io_serial_out(struct uart_port *p, int offset, int value)
423 offset = offset << p->regshift;
424 outb(value, p->iobase + offset);
427 static int serial8250_default_handle_irq(struct uart_port *port);
428 static int exar_handle_irq(struct uart_port *port);
430 static void set_io_from_upio(struct uart_port *p)
432 struct uart_8250_port *up = up_to_u8250p(p);
434 up->dl_read = default_serial_dl_read;
435 up->dl_write = default_serial_dl_write;
439 p->serial_in = hub6_serial_in;
440 p->serial_out = hub6_serial_out;
444 p->serial_in = mem_serial_in;
445 p->serial_out = mem_serial_out;
449 p->serial_in = mem16_serial_in;
450 p->serial_out = mem16_serial_out;
454 p->serial_in = mem32_serial_in;
455 p->serial_out = mem32_serial_out;
459 p->serial_in = mem32be_serial_in;
460 p->serial_out = mem32be_serial_out;
463 #ifdef CONFIG_SERIAL_8250_RT288X
465 p->serial_in = au_serial_in;
466 p->serial_out = au_serial_out;
467 up->dl_read = au_serial_dl_read;
468 up->dl_write = au_serial_dl_write;
473 p->serial_in = io_serial_in;
474 p->serial_out = io_serial_out;
477 /* Remember loaded iotype */
478 up->cur_iotype = p->iotype;
479 p->handle_irq = serial8250_default_handle_irq;
483 serial_port_out_sync(struct uart_port *p, int offset, int value)
491 p->serial_out(p, offset, value);
492 p->serial_in(p, UART_LCR); /* safe, no side-effects */
495 p->serial_out(p, offset, value);
502 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
504 serial_out(up, UART_SCR, offset);
505 serial_out(up, UART_ICR, value);
508 static unsigned int serial_icr_read(struct uart_8250_port *up, int offset)
512 serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
513 serial_out(up, UART_SCR, offset);
514 value = serial_in(up, UART_ICR);
515 serial_icr_write(up, UART_ACR, up->acr);
523 static void serial8250_clear_fifos(struct uart_8250_port *p)
525 if (p->capabilities & UART_CAP_FIFO) {
526 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
527 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
528 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
529 serial_out(p, UART_FCR, 0);
533 static inline void serial8250_em485_rts_after_send(struct uart_8250_port *p)
535 unsigned char mcr = serial8250_in_MCR(p);
537 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
540 mcr &= ~UART_MCR_RTS;
541 serial8250_out_MCR(p, mcr);
544 static void serial8250_em485_handle_start_tx(unsigned long arg);
545 static void serial8250_em485_handle_stop_tx(unsigned long arg);
547 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
549 serial8250_clear_fifos(p);
550 serial_out(p, UART_FCR, p->fcr);
552 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
554 void serial8250_rpm_get(struct uart_8250_port *p)
556 if (!(p->capabilities & UART_CAP_RPM))
558 pm_runtime_get_sync(p->port.dev);
560 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
562 void serial8250_rpm_put(struct uart_8250_port *p)
564 if (!(p->capabilities & UART_CAP_RPM))
566 pm_runtime_mark_last_busy(p->port.dev);
567 pm_runtime_put_autosuspend(p->port.dev);
569 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
572 * serial8250_em485_init() - put uart_8250_port into rs485 emulating
573 * @p: uart_8250_port port instance
575 * The function is used to start rs485 software emulating on the
576 * &struct uart_8250_port* @p. Namely, RTS is switched before/after
577 * transmission. The function is idempotent, so it is safe to call it
580 * The caller MUST enable interrupt on empty shift register before
581 * calling serial8250_em485_init(). This interrupt is not a part of
582 * 8250 standard, but implementation defined.
584 * The function is supposed to be called from .rs485_config callback
585 * or from any other callback protected with p->port.lock spinlock.
587 * See also serial8250_em485_destroy()
589 * Return 0 - success, -errno - otherwise
591 int serial8250_em485_init(struct uart_8250_port *p)
596 p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC);
600 setup_timer(&p->em485->stop_tx_timer,
601 serial8250_em485_handle_stop_tx, (unsigned long)p);
602 setup_timer(&p->em485->start_tx_timer,
603 serial8250_em485_handle_start_tx, (unsigned long)p);
604 p->em485->active_timer = NULL;
606 serial8250_em485_rts_after_send(p);
610 EXPORT_SYMBOL_GPL(serial8250_em485_init);
613 * serial8250_em485_destroy() - put uart_8250_port into normal state
614 * @p: uart_8250_port port instance
616 * The function is used to stop rs485 software emulating on the
617 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to
618 * call it multiple times.
620 * The function is supposed to be called from .rs485_config callback
621 * or from any other callback protected with p->port.lock spinlock.
623 * See also serial8250_em485_init()
625 void serial8250_em485_destroy(struct uart_8250_port *p)
630 del_timer(&p->em485->start_tx_timer);
631 del_timer(&p->em485->stop_tx_timer);
636 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
639 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
640 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
641 * empty and the HW can idle again.
643 static void serial8250_rpm_get_tx(struct uart_8250_port *p)
645 unsigned char rpm_active;
647 if (!(p->capabilities & UART_CAP_RPM))
650 rpm_active = xchg(&p->rpm_tx_active, 1);
653 pm_runtime_get_sync(p->port.dev);
656 static void serial8250_rpm_put_tx(struct uart_8250_port *p)
658 unsigned char rpm_active;
660 if (!(p->capabilities & UART_CAP_RPM))
663 rpm_active = xchg(&p->rpm_tx_active, 0);
666 pm_runtime_mark_last_busy(p->port.dev);
667 pm_runtime_put_autosuspend(p->port.dev);
671 * IER sleep support. UARTs which have EFRs need the "extended
672 * capability" bit enabled. Note that on XR16C850s, we need to
673 * reset LCR to write to IER.
675 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
677 unsigned char lcr = 0, efr = 0;
679 * Exar UARTs have a SLEEP register that enables or disables
680 * each UART to enter sleep mode separately. On the XR17V35x the
681 * register is accessible to each UART at the UART_EXAR_SLEEP
682 * offset but the UART channel may only write to the corresponding
685 serial8250_rpm_get(p);
686 if ((p->port.type == PORT_XR17V35X) ||
687 (p->port.type == PORT_XR17D15X)) {
688 serial_out(p, UART_EXAR_SLEEP, sleep ? 0xff : 0);
692 if (p->capabilities & UART_CAP_SLEEP) {
693 if (p->capabilities & UART_CAP_EFR) {
694 lcr = serial_in(p, UART_LCR);
695 efr = serial_in(p, UART_EFR);
696 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
697 serial_out(p, UART_EFR, UART_EFR_ECB);
698 serial_out(p, UART_LCR, 0);
700 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
701 if (p->capabilities & UART_CAP_EFR) {
702 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
703 serial_out(p, UART_EFR, efr);
704 serial_out(p, UART_LCR, lcr);
708 serial8250_rpm_put(p);
711 #ifdef CONFIG_SERIAL_8250_RSA
713 * Attempts to turn on the RSA FIFO. Returns zero on failure.
714 * We set the port uart clock rate if we succeed.
716 static int __enable_rsa(struct uart_8250_port *up)
721 mode = serial_in(up, UART_RSA_MSR);
722 result = mode & UART_RSA_MSR_FIFO;
725 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
726 mode = serial_in(up, UART_RSA_MSR);
727 result = mode & UART_RSA_MSR_FIFO;
731 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
736 static void enable_rsa(struct uart_8250_port *up)
738 if (up->port.type == PORT_RSA) {
739 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
740 spin_lock_irq(&up->port.lock);
742 spin_unlock_irq(&up->port.lock);
744 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
745 serial_out(up, UART_RSA_FRR, 0);
750 * Attempts to turn off the RSA FIFO. Returns zero on failure.
751 * It is unknown why interrupts were disabled in here. However,
752 * the caller is expected to preserve this behaviour by grabbing
753 * the spinlock before calling this function.
755 static void disable_rsa(struct uart_8250_port *up)
760 if (up->port.type == PORT_RSA &&
761 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
762 spin_lock_irq(&up->port.lock);
764 mode = serial_in(up, UART_RSA_MSR);
765 result = !(mode & UART_RSA_MSR_FIFO);
768 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
769 mode = serial_in(up, UART_RSA_MSR);
770 result = !(mode & UART_RSA_MSR_FIFO);
774 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
775 spin_unlock_irq(&up->port.lock);
778 #endif /* CONFIG_SERIAL_8250_RSA */
781 * This is a quickie test to see how big the FIFO is.
782 * It doesn't work at all the time, more's the pity.
784 static int size_fifo(struct uart_8250_port *up)
786 unsigned char old_fcr, old_mcr, old_lcr;
787 unsigned short old_dl;
790 old_lcr = serial_in(up, UART_LCR);
791 serial_out(up, UART_LCR, 0);
792 old_fcr = serial_in(up, UART_FCR);
793 old_mcr = serial8250_in_MCR(up);
794 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
795 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
796 serial8250_out_MCR(up, UART_MCR_LOOP);
797 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
798 old_dl = serial_dl_read(up);
799 serial_dl_write(up, 0x0001);
800 serial_out(up, UART_LCR, 0x03);
801 for (count = 0; count < 256; count++)
802 serial_out(up, UART_TX, count);
803 mdelay(20);/* FIXME - schedule_timeout */
804 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
805 (count < 256); count++)
806 serial_in(up, UART_RX);
807 serial_out(up, UART_FCR, old_fcr);
808 serial8250_out_MCR(up, old_mcr);
809 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
810 serial_dl_write(up, old_dl);
811 serial_out(up, UART_LCR, old_lcr);
817 * Read UART ID using the divisor method - set DLL and DLM to zero
818 * and the revision will be in DLL and device type in DLM. We
819 * preserve the device state across this.
821 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
823 unsigned char old_lcr;
824 unsigned int id, old_dl;
826 old_lcr = serial_in(p, UART_LCR);
827 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
828 old_dl = serial_dl_read(p);
829 serial_dl_write(p, 0);
830 id = serial_dl_read(p);
831 serial_dl_write(p, old_dl);
833 serial_out(p, UART_LCR, old_lcr);
839 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
840 * When this function is called we know it is at least a StarTech
841 * 16650 V2, but it might be one of several StarTech UARTs, or one of
842 * its clones. (We treat the broken original StarTech 16650 V1 as a
843 * 16550, and why not? Startech doesn't seem to even acknowledge its
846 * What evil have men's minds wrought...
848 static void autoconfig_has_efr(struct uart_8250_port *up)
850 unsigned int id1, id2, id3, rev;
853 * Everything with an EFR has SLEEP
855 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
858 * First we check to see if it's an Oxford Semiconductor UART.
860 * If we have to do this here because some non-National
861 * Semiconductor clone chips lock up if you try writing to the
862 * LSR register (which serial_icr_read does)
866 * Check for Oxford Semiconductor 16C950.
868 * EFR [4] must be set else this test fails.
870 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
871 * claims that it's needed for 952 dual UART's (which are not
872 * recommended for new designs).
875 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
876 serial_out(up, UART_EFR, UART_EFR_ECB);
877 serial_out(up, UART_LCR, 0x00);
878 id1 = serial_icr_read(up, UART_ID1);
879 id2 = serial_icr_read(up, UART_ID2);
880 id3 = serial_icr_read(up, UART_ID3);
881 rev = serial_icr_read(up, UART_REV);
883 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
885 if (id1 == 0x16 && id2 == 0xC9 &&
886 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
887 up->port.type = PORT_16C950;
890 * Enable work around for the Oxford Semiconductor 952 rev B
891 * chip which causes it to seriously miscalculate baud rates
894 if (id3 == 0x52 && rev == 0x01)
895 up->bugs |= UART_BUG_QUOT;
900 * We check for a XR16C850 by setting DLL and DLM to 0, and then
901 * reading back DLL and DLM. The chip type depends on the DLM
903 * 0x10 - XR16C850 and the DLL contains the chip revision.
907 id1 = autoconfig_read_divisor_id(up);
908 DEBUG_AUTOCONF("850id=%04x ", id1);
911 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
912 up->port.type = PORT_16850;
917 * It wasn't an XR16C850.
919 * We distinguish between the '654 and the '650 by counting
920 * how many bytes are in the FIFO. I'm using this for now,
921 * since that's the technique that was sent to me in the
922 * serial driver update, but I'm not convinced this works.
923 * I've had problems doing this in the past. -TYT
925 if (size_fifo(up) == 64)
926 up->port.type = PORT_16654;
928 up->port.type = PORT_16650V2;
932 * We detected a chip without a FIFO. Only two fall into
933 * this category - the original 8250 and the 16450. The
934 * 16450 has a scratch register (accessible with LCR=0)
936 static void autoconfig_8250(struct uart_8250_port *up)
938 unsigned char scratch, status1, status2;
940 up->port.type = PORT_8250;
942 scratch = serial_in(up, UART_SCR);
943 serial_out(up, UART_SCR, 0xa5);
944 status1 = serial_in(up, UART_SCR);
945 serial_out(up, UART_SCR, 0x5a);
946 status2 = serial_in(up, UART_SCR);
947 serial_out(up, UART_SCR, scratch);
949 if (status1 == 0xa5 && status2 == 0x5a)
950 up->port.type = PORT_16450;
953 static int broken_efr(struct uart_8250_port *up)
956 * Exar ST16C2550 "A2" devices incorrectly detect as
957 * having an EFR, and report an ID of 0x0201. See
958 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
960 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
967 * We know that the chip has FIFOs. Does it have an EFR? The
968 * EFR is located in the same register position as the IIR and
969 * we know the top two bits of the IIR are currently set. The
970 * EFR should contain zero. Try to read the EFR.
972 static void autoconfig_16550a(struct uart_8250_port *up)
974 unsigned char status1, status2;
975 unsigned int iersave;
977 up->port.type = PORT_16550A;
978 up->capabilities |= UART_CAP_FIFO;
981 * XR17V35x UARTs have an extra divisor register, DLD
982 * that gets enabled with when DLAB is set which will
983 * cause the device to incorrectly match and assign
984 * port type to PORT_16650. The EFR for this UART is
985 * found at offset 0x09. Instead check the Deice ID (DVID)
986 * register for a 2, 4 or 8 port UART.
988 if (up->port.flags & UPF_EXAR_EFR) {
989 status1 = serial_in(up, UART_EXAR_DVID);
990 if (status1 == 0x82 || status1 == 0x84 || status1 == 0x88) {
991 DEBUG_AUTOCONF("Exar XR17V35x ");
992 up->port.type = PORT_XR17V35X;
993 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1002 * Check for presence of the EFR when DLAB is set.
1003 * Only ST16C650V1 UARTs pass this test.
1005 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1006 if (serial_in(up, UART_EFR) == 0) {
1007 serial_out(up, UART_EFR, 0xA8);
1008 if (serial_in(up, UART_EFR) != 0) {
1009 DEBUG_AUTOCONF("EFRv1 ");
1010 up->port.type = PORT_16650;
1011 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1013 serial_out(up, UART_LCR, 0);
1014 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1016 status1 = serial_in(up, UART_IIR) >> 5;
1017 serial_out(up, UART_FCR, 0);
1018 serial_out(up, UART_LCR, 0);
1021 up->port.type = PORT_16550A_FSL64;
1023 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1025 serial_out(up, UART_EFR, 0);
1030 * Maybe it requires 0xbf to be written to the LCR.
1031 * (other ST16C650V2 UARTs, TI16C752A, etc)
1033 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1034 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1035 DEBUG_AUTOCONF("EFRv2 ");
1036 autoconfig_has_efr(up);
1041 * Check for a National Semiconductor SuperIO chip.
1042 * Attempt to switch to bank 2, read the value of the LOOP bit
1043 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1044 * switch back to bank 2, read it from EXCR1 again and check
1045 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1047 serial_out(up, UART_LCR, 0);
1048 status1 = serial8250_in_MCR(up);
1049 serial_out(up, UART_LCR, 0xE0);
1050 status2 = serial_in(up, 0x02); /* EXCR1 */
1052 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1053 serial_out(up, UART_LCR, 0);
1054 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
1055 serial_out(up, UART_LCR, 0xE0);
1056 status2 = serial_in(up, 0x02); /* EXCR1 */
1057 serial_out(up, UART_LCR, 0);
1058 serial8250_out_MCR(up, status1);
1060 if ((status2 ^ status1) & UART_MCR_LOOP) {
1061 unsigned short quot;
1063 serial_out(up, UART_LCR, 0xE0);
1065 quot = serial_dl_read(up);
1068 if (ns16550a_goto_highspeed(up))
1069 serial_dl_write(up, quot);
1071 serial_out(up, UART_LCR, 0);
1073 up->port.uartclk = 921600*16;
1074 up->port.type = PORT_NS16550A;
1075 up->capabilities |= UART_NATSEMI;
1081 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1082 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1083 * Try setting it with and without DLAB set. Cheap clones
1084 * set bit 5 without DLAB set.
1086 serial_out(up, UART_LCR, 0);
1087 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1088 status1 = serial_in(up, UART_IIR) >> 5;
1089 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1090 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1091 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1092 status2 = serial_in(up, UART_IIR) >> 5;
1093 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1094 serial_out(up, UART_LCR, 0);
1096 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1098 if (status1 == 6 && status2 == 7) {
1099 up->port.type = PORT_16750;
1100 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1105 * Try writing and reading the UART_IER_UUE bit (b6).
1106 * If it works, this is probably one of the Xscale platform's
1108 * We're going to explicitly set the UUE bit to 0 before
1109 * trying to write and read a 1 just to make sure it's not
1110 * already a 1 and maybe locked there before we even start start.
1112 iersave = serial_in(up, UART_IER);
1113 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1114 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1116 * OK it's in a known zero state, try writing and reading
1117 * without disturbing the current state of the other bits.
1119 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1120 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1123 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1125 DEBUG_AUTOCONF("Xscale ");
1126 up->port.type = PORT_XSCALE;
1127 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1132 * If we got here we couldn't force the IER_UUE bit to 0.
1133 * Log it and continue.
1135 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1137 serial_out(up, UART_IER, iersave);
1140 * Exar uarts have EFR in a weird location
1142 if (up->port.flags & UPF_EXAR_EFR) {
1143 DEBUG_AUTOCONF("Exar XR17D15x ");
1144 up->port.type = PORT_XR17D15X;
1145 up->capabilities |= UART_CAP_AFE | UART_CAP_EFR |
1152 * We distinguish between 16550A and U6 16550A by counting
1153 * how many bytes are in the FIFO.
1155 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1156 up->port.type = PORT_U6_16550A;
1157 up->capabilities |= UART_CAP_AFE;
1162 * This routine is called by rs_init() to initialize a specific serial
1163 * port. It determines what type of UART chip this serial port is
1164 * using: 8250, 16450, 16550, 16550A. The important question is
1165 * whether or not this UART is a 16550A or not, since this will
1166 * determine whether or not we can use its FIFO features or not.
1168 static void autoconfig(struct uart_8250_port *up)
1170 unsigned char status1, scratch, scratch2, scratch3;
1171 unsigned char save_lcr, save_mcr;
1172 struct uart_port *port = &up->port;
1173 unsigned long flags;
1174 unsigned int old_capabilities;
1176 if (!port->iobase && !port->mapbase && !port->membase)
1179 DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04lx, 0x%p): ",
1180 serial_index(port), port->iobase, port->membase);
1183 * We really do need global IRQs disabled here - we're going to
1184 * be frobbing the chips IRQ enable register to see if it exists.
1186 spin_lock_irqsave(&port->lock, flags);
1188 up->capabilities = 0;
1191 if (!(port->flags & UPF_BUGGY_UART)) {
1193 * Do a simple existence test first; if we fail this,
1194 * there's no point trying anything else.
1196 * 0x80 is used as a nonsense port to prevent against
1197 * false positives due to ISA bus float. The
1198 * assumption is that 0x80 is a non-existent port;
1199 * which should be safe since include/asm/io.h also
1200 * makes this assumption.
1202 * Note: this is safe as long as MCR bit 4 is clear
1203 * and the device is in "PC" mode.
1205 scratch = serial_in(up, UART_IER);
1206 serial_out(up, UART_IER, 0);
1211 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1212 * 16C754B) allow only to modify them if an EFR bit is set.
1214 scratch2 = serial_in(up, UART_IER) & 0x0f;
1215 serial_out(up, UART_IER, 0x0F);
1219 scratch3 = serial_in(up, UART_IER) & 0x0f;
1220 serial_out(up, UART_IER, scratch);
1221 if (scratch2 != 0 || scratch3 != 0x0F) {
1223 * We failed; there's nothing here
1225 spin_unlock_irqrestore(&port->lock, flags);
1226 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1227 scratch2, scratch3);
1232 save_mcr = serial8250_in_MCR(up);
1233 save_lcr = serial_in(up, UART_LCR);
1236 * Check to see if a UART is really there. Certain broken
1237 * internal modems based on the Rockwell chipset fail this
1238 * test, because they apparently don't implement the loopback
1239 * test mode. So this test is skipped on the COM 1 through
1240 * COM 4 ports. This *should* be safe, since no board
1241 * manufacturer would be stupid enough to design a board
1242 * that conflicts with COM 1-4 --- we hope!
1244 if (!(port->flags & UPF_SKIP_TEST)) {
1245 serial8250_out_MCR(up, UART_MCR_LOOP | 0x0A);
1246 status1 = serial_in(up, UART_MSR) & 0xF0;
1247 serial8250_out_MCR(up, save_mcr);
1248 if (status1 != 0x90) {
1249 spin_unlock_irqrestore(&port->lock, flags);
1250 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1257 * We're pretty sure there's a port here. Lets find out what
1258 * type of port it is. The IIR top two bits allows us to find
1259 * out if it's 8250 or 16450, 16550, 16550A or later. This
1260 * determines what we test for next.
1262 * We also initialise the EFR (if any) to zero for later. The
1263 * EFR occupies the same register location as the FCR and IIR.
1265 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1266 serial_out(up, UART_EFR, 0);
1267 serial_out(up, UART_LCR, 0);
1269 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1271 /* Assign this as it is to truncate any bits above 7. */
1272 scratch = serial_in(up, UART_IIR);
1274 switch (scratch >> 6) {
1276 autoconfig_8250(up);
1279 port->type = PORT_UNKNOWN;
1282 port->type = PORT_16550;
1285 autoconfig_16550a(up);
1289 #ifdef CONFIG_SERIAL_8250_RSA
1291 * Only probe for RSA ports if we got the region.
1293 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1295 port->type = PORT_RSA;
1298 serial_out(up, UART_LCR, save_lcr);
1300 port->fifosize = uart_config[up->port.type].fifo_size;
1301 old_capabilities = up->capabilities;
1302 up->capabilities = uart_config[port->type].flags;
1303 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1305 if (port->type == PORT_UNKNOWN)
1311 #ifdef CONFIG_SERIAL_8250_RSA
1312 if (port->type == PORT_RSA)
1313 serial_out(up, UART_RSA_FRR, 0);
1315 serial8250_out_MCR(up, save_mcr);
1316 serial8250_clear_fifos(up);
1317 serial_in(up, UART_RX);
1318 if (up->capabilities & UART_CAP_UUE)
1319 serial_out(up, UART_IER, UART_IER_UUE);
1321 serial_out(up, UART_IER, 0);
1324 spin_unlock_irqrestore(&port->lock, flags);
1327 * Check if the device is a Fintek F81216A
1329 if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1330 fintek_8250_probe(up);
1332 if (up->capabilities != old_capabilities) {
1333 pr_warn("ttyS%d: detected caps %08x should be %08x\n",
1334 serial_index(port), old_capabilities,
1338 DEBUG_AUTOCONF("iir=%d ", scratch);
1339 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1342 static void autoconfig_irq(struct uart_8250_port *up)
1344 struct uart_port *port = &up->port;
1345 unsigned char save_mcr, save_ier;
1346 unsigned char save_ICP = 0;
1347 unsigned int ICP = 0;
1351 if (port->flags & UPF_FOURPORT) {
1352 ICP = (port->iobase & 0xfe0) | 0x1f;
1353 save_ICP = inb_p(ICP);
1358 if (uart_console(port))
1361 /* forget possible initially masked and pending IRQ */
1362 probe_irq_off(probe_irq_on());
1363 save_mcr = serial8250_in_MCR(up);
1364 save_ier = serial_in(up, UART_IER);
1365 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1367 irqs = probe_irq_on();
1368 serial8250_out_MCR(up, 0);
1370 if (port->flags & UPF_FOURPORT) {
1371 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1373 serial8250_out_MCR(up,
1374 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1376 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1377 serial_in(up, UART_LSR);
1378 serial_in(up, UART_RX);
1379 serial_in(up, UART_IIR);
1380 serial_in(up, UART_MSR);
1381 serial_out(up, UART_TX, 0xFF);
1383 irq = probe_irq_off(irqs);
1385 serial8250_out_MCR(up, save_mcr);
1386 serial_out(up, UART_IER, save_ier);
1388 if (port->flags & UPF_FOURPORT)
1389 outb_p(save_ICP, ICP);
1391 if (uart_console(port))
1394 port->irq = (irq > 0) ? irq : 0;
1397 static void serial8250_stop_rx(struct uart_port *port)
1399 struct uart_8250_port *up = up_to_u8250p(port);
1401 serial8250_rpm_get(up);
1403 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1404 up->port.read_status_mask &= ~UART_LSR_DR;
1405 serial_port_out(port, UART_IER, up->ier);
1407 serial8250_rpm_put(up);
1410 static void __do_stop_tx_rs485(struct uart_8250_port *p)
1412 serial8250_em485_rts_after_send(p);
1415 * Empty the RX FIFO, we are not interested in anything
1416 * received during the half-duplex transmission.
1417 * Enable previously disabled RX interrupts.
1419 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1420 serial8250_clear_and_reinit_fifos(p);
1422 p->ier |= UART_IER_RLSI | UART_IER_RDI;
1423 serial_port_out(&p->port, UART_IER, p->ier);
1427 static void serial8250_em485_handle_stop_tx(unsigned long arg)
1429 struct uart_8250_port *p = (struct uart_8250_port *)arg;
1430 struct uart_8250_em485 *em485 = p->em485;
1431 unsigned long flags;
1433 serial8250_rpm_get(p);
1434 spin_lock_irqsave(&p->port.lock, flags);
1436 em485->active_timer == &em485->stop_tx_timer) {
1437 __do_stop_tx_rs485(p);
1438 em485->active_timer = NULL;
1440 spin_unlock_irqrestore(&p->port.lock, flags);
1441 serial8250_rpm_put(p);
1444 static void __stop_tx_rs485(struct uart_8250_port *p)
1446 struct uart_8250_em485 *em485 = p->em485;
1449 * __do_stop_tx_rs485 is going to set RTS according to config
1450 * AND flush RX FIFO if required.
1452 if (p->port.rs485.delay_rts_after_send > 0) {
1453 em485->active_timer = &em485->stop_tx_timer;
1454 mod_timer(&em485->stop_tx_timer, jiffies +
1455 p->port.rs485.delay_rts_after_send * HZ / 1000);
1457 __do_stop_tx_rs485(p);
1461 static inline void __do_stop_tx(struct uart_8250_port *p)
1463 if (p->ier & UART_IER_THRI) {
1464 p->ier &= ~UART_IER_THRI;
1465 serial_out(p, UART_IER, p->ier);
1466 serial8250_rpm_put_tx(p);
1470 static inline void __stop_tx(struct uart_8250_port *p)
1472 struct uart_8250_em485 *em485 = p->em485;
1475 unsigned char lsr = serial_in(p, UART_LSR);
1476 p->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1479 * To provide required timeing and allow FIFO transfer,
1480 * __stop_tx_rs485() must be called only when both FIFO and
1481 * shift register are empty. It is for device driver to enable
1482 * interrupt on TEMT.
1484 if ((lsr & BOTH_EMPTY) != BOTH_EMPTY)
1487 del_timer(&em485->start_tx_timer);
1488 em485->active_timer = NULL;
1495 static void serial8250_stop_tx(struct uart_port *port)
1497 struct uart_8250_port *up = up_to_u8250p(port);
1499 serial8250_rpm_get(up);
1503 * We really want to stop the transmitter from sending.
1505 if (port->type == PORT_16C950) {
1506 up->acr |= UART_ACR_TXDIS;
1507 serial_icr_write(up, UART_ACR, up->acr);
1509 serial8250_rpm_put(up);
1512 static inline void __start_tx(struct uart_port *port)
1514 struct uart_8250_port *up = up_to_u8250p(port);
1516 if (up->dma && !up->dma->tx_dma(up))
1519 if (!(up->ier & UART_IER_THRI)) {
1520 up->ier |= UART_IER_THRI;
1521 serial_port_out(port, UART_IER, up->ier);
1523 if (up->bugs & UART_BUG_TXEN) {
1526 lsr = serial_in(up, UART_LSR);
1527 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1528 if (lsr & UART_LSR_THRE)
1529 serial8250_tx_chars(up);
1534 * Re-enable the transmitter if we disabled it.
1536 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1537 up->acr &= ~UART_ACR_TXDIS;
1538 serial_icr_write(up, UART_ACR, up->acr);
1542 static inline void start_tx_rs485(struct uart_port *port)
1544 struct uart_8250_port *up = up_to_u8250p(port);
1545 struct uart_8250_em485 *em485 = up->em485;
1548 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1549 serial8250_stop_rx(&up->port);
1551 del_timer(&em485->stop_tx_timer);
1552 em485->active_timer = NULL;
1554 mcr = serial8250_in_MCR(up);
1555 if (!!(up->port.rs485.flags & SER_RS485_RTS_ON_SEND) !=
1556 !!(mcr & UART_MCR_RTS)) {
1557 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1558 mcr |= UART_MCR_RTS;
1560 mcr &= ~UART_MCR_RTS;
1561 serial8250_out_MCR(up, mcr);
1563 if (up->port.rs485.delay_rts_before_send > 0) {
1564 em485->active_timer = &em485->start_tx_timer;
1565 mod_timer(&em485->start_tx_timer, jiffies +
1566 up->port.rs485.delay_rts_before_send * HZ / 1000);
1574 static void serial8250_em485_handle_start_tx(unsigned long arg)
1576 struct uart_8250_port *p = (struct uart_8250_port *)arg;
1577 struct uart_8250_em485 *em485 = p->em485;
1578 unsigned long flags;
1580 spin_lock_irqsave(&p->port.lock, flags);
1582 em485->active_timer == &em485->start_tx_timer) {
1583 __start_tx(&p->port);
1584 em485->active_timer = NULL;
1586 spin_unlock_irqrestore(&p->port.lock, flags);
1589 static void serial8250_start_tx(struct uart_port *port)
1591 struct uart_8250_port *up = up_to_u8250p(port);
1592 struct uart_8250_em485 *em485 = up->em485;
1594 serial8250_rpm_get_tx(up);
1597 em485->active_timer == &em485->start_tx_timer)
1601 start_tx_rs485(port);
1606 static void serial8250_throttle(struct uart_port *port)
1608 port->throttle(port);
1611 static void serial8250_unthrottle(struct uart_port *port)
1613 port->unthrottle(port);
1616 static void serial8250_disable_ms(struct uart_port *port)
1618 struct uart_8250_port *up = up_to_u8250p(port);
1620 /* no MSR capabilities */
1621 if (up->bugs & UART_BUG_NOMSR)
1624 up->ier &= ~UART_IER_MSI;
1625 serial_port_out(port, UART_IER, up->ier);
1628 static void serial8250_enable_ms(struct uart_port *port)
1630 struct uart_8250_port *up = up_to_u8250p(port);
1632 /* no MSR capabilities */
1633 if (up->bugs & UART_BUG_NOMSR)
1636 up->ier |= UART_IER_MSI;
1638 serial8250_rpm_get(up);
1639 serial_port_out(port, UART_IER, up->ier);
1640 serial8250_rpm_put(up);
1643 static void serial8250_read_char(struct uart_8250_port *up, unsigned char lsr)
1645 struct uart_port *port = &up->port;
1647 char flag = TTY_NORMAL;
1649 if (likely(lsr & UART_LSR_DR))
1650 ch = serial_in(up, UART_RX);
1653 * Intel 82571 has a Serial Over Lan device that will
1654 * set UART_LSR_BI without setting UART_LSR_DR when
1655 * it receives a break. To avoid reading from the
1656 * receive buffer without UART_LSR_DR bit set, we
1657 * just force the read character to be 0
1663 lsr |= up->lsr_saved_flags;
1664 up->lsr_saved_flags = 0;
1666 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1667 if (lsr & UART_LSR_BI) {
1668 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1671 * We do the SysRQ and SAK checking
1672 * here because otherwise the break
1673 * may get masked by ignore_status_mask
1674 * or read_status_mask.
1676 if (uart_handle_break(port))
1678 } else if (lsr & UART_LSR_PE)
1679 port->icount.parity++;
1680 else if (lsr & UART_LSR_FE)
1681 port->icount.frame++;
1682 if (lsr & UART_LSR_OE)
1683 port->icount.overrun++;
1686 * Mask off conditions which should be ignored.
1688 lsr &= port->read_status_mask;
1690 if (lsr & UART_LSR_BI) {
1691 pr_debug("%s: handling break\n", __func__);
1693 } else if (lsr & UART_LSR_PE)
1695 else if (lsr & UART_LSR_FE)
1698 if (uart_handle_sysrq_char(port, ch))
1701 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1705 * serial8250_rx_chars: processes according to the passed in LSR
1706 * value, and returns the remaining LSR bits not handled
1707 * by this Rx routine.
1709 unsigned char serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1711 struct uart_port *port = &up->port;
1712 int max_count = 256;
1715 serial8250_read_char(up, lsr);
1716 if (--max_count == 0)
1718 lsr = serial_in(up, UART_LSR);
1719 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
1721 tty_flip_buffer_push(&port->state->port);
1724 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1726 void serial8250_tx_chars(struct uart_8250_port *up)
1728 struct uart_port *port = &up->port;
1729 struct circ_buf *xmit = &port->state->xmit;
1733 serial_out(up, UART_TX, port->x_char);
1738 if (uart_tx_stopped(port)) {
1739 serial8250_stop_tx(port);
1742 if (uart_circ_empty(xmit)) {
1747 count = up->tx_loadsz;
1749 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1750 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1752 if (uart_circ_empty(xmit))
1754 if ((up->capabilities & UART_CAP_HFIFO) &&
1755 (serial_in(up, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY)
1757 } while (--count > 0);
1759 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1760 uart_write_wakeup(port);
1763 * With RPM enabled, we have to wait until the FIFO is empty before the
1764 * HW can go idle. So we get here once again with empty FIFO and disable
1765 * the interrupt and RPM in __stop_tx()
1767 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1770 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1772 /* Caller holds uart port lock */
1773 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1775 struct uart_port *port = &up->port;
1776 unsigned int status = serial_in(up, UART_MSR);
1778 status |= up->msr_saved_flags;
1779 up->msr_saved_flags = 0;
1780 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1781 port->state != NULL) {
1782 if (status & UART_MSR_TERI)
1784 if (status & UART_MSR_DDSR)
1786 if (status & UART_MSR_DDCD)
1787 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1788 if (status & UART_MSR_DCTS)
1789 uart_handle_cts_change(port, status & UART_MSR_CTS);
1791 wake_up_interruptible(&port->state->port.delta_msr_wait);
1796 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1798 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1800 switch (iir & 0x3f) {
1801 case UART_IIR_RX_TIMEOUT:
1802 serial8250_rx_dma_flush(up);
1807 return up->dma->rx_dma(up);
1811 * This handles the interrupt from one port.
1813 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1815 unsigned char status;
1816 unsigned long flags;
1817 struct uart_8250_port *up = up_to_u8250p(port);
1818 bool skip_rx = false;
1820 if (iir & UART_IIR_NO_INT)
1823 spin_lock_irqsave(&port->lock, flags);
1825 status = serial_port_in(port, UART_LSR);
1828 * If port is stopped and there are no error conditions in the
1829 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer
1830 * overflow. Not servicing, RX FIFO would trigger auto HW flow
1831 * control when FIFO occupancy reaches preset threshold, thus
1832 * halting RX. This only works when auto HW flow control is
1835 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) &&
1836 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) &&
1837 !(port->read_status_mask & UART_LSR_DR))
1840 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) {
1841 if (!up->dma || handle_rx_dma(up, iir))
1842 status = serial8250_rx_chars(up, status);
1844 serial8250_modem_status(up);
1845 if ((!up->dma || up->dma->tx_err) && (status & UART_LSR_THRE) &&
1846 (up->ier & UART_IER_THRI))
1847 serial8250_tx_chars(up);
1849 spin_unlock_irqrestore(&port->lock, flags);
1852 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1854 static int serial8250_default_handle_irq(struct uart_port *port)
1856 struct uart_8250_port *up = up_to_u8250p(port);
1860 serial8250_rpm_get(up);
1862 iir = serial_port_in(port, UART_IIR);
1863 ret = serial8250_handle_irq(port, iir);
1865 serial8250_rpm_put(up);
1870 * These Exar UARTs have an extra interrupt indicator that could
1871 * fire for a few unimplemented interrupts. One of which is a
1872 * wakeup event when coming out of sleep. Put this here just
1873 * to be on the safe side that these interrupts don't go unhandled.
1875 static int exar_handle_irq(struct uart_port *port)
1877 unsigned int iir = serial_port_in(port, UART_IIR);
1880 ret = serial8250_handle_irq(port, iir);
1882 if ((port->type == PORT_XR17V35X) ||
1883 (port->type == PORT_XR17D15X)) {
1884 serial_port_in(port, 0x80);
1885 serial_port_in(port, 0x81);
1886 serial_port_in(port, 0x82);
1887 serial_port_in(port, 0x83);
1894 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1895 * have a programmable TX threshold that triggers the THRE interrupt in
1896 * the IIR register. In this case, the THRE interrupt indicates the FIFO
1897 * has space available. Load it up with tx_loadsz bytes.
1899 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1901 unsigned long flags;
1902 unsigned int iir = serial_port_in(port, UART_IIR);
1904 /* TX Threshold IRQ triggered so load up FIFO */
1905 if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1906 struct uart_8250_port *up = up_to_u8250p(port);
1908 spin_lock_irqsave(&port->lock, flags);
1909 serial8250_tx_chars(up);
1910 spin_unlock_irqrestore(&port->lock, flags);
1913 iir = serial_port_in(port, UART_IIR);
1914 return serial8250_handle_irq(port, iir);
1917 static unsigned int serial8250_tx_empty(struct uart_port *port)
1919 struct uart_8250_port *up = up_to_u8250p(port);
1920 unsigned long flags;
1923 serial8250_rpm_get(up);
1925 spin_lock_irqsave(&port->lock, flags);
1926 lsr = serial_port_in(port, UART_LSR);
1927 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1928 spin_unlock_irqrestore(&port->lock, flags);
1930 serial8250_rpm_put(up);
1932 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1935 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
1937 struct uart_8250_port *up = up_to_u8250p(port);
1938 unsigned int status;
1941 serial8250_rpm_get(up);
1942 status = serial8250_modem_status(up);
1943 serial8250_rpm_put(up);
1946 if (status & UART_MSR_DCD)
1948 if (status & UART_MSR_RI)
1950 if (status & UART_MSR_DSR)
1952 if (status & UART_MSR_CTS)
1956 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
1958 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1960 if (port->get_mctrl)
1961 return port->get_mctrl(port);
1962 return serial8250_do_get_mctrl(port);
1965 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1967 struct uart_8250_port *up = up_to_u8250p(port);
1968 unsigned char mcr = 0;
1970 if (mctrl & TIOCM_RTS)
1971 mcr |= UART_MCR_RTS;
1972 if (mctrl & TIOCM_DTR)
1973 mcr |= UART_MCR_DTR;
1974 if (mctrl & TIOCM_OUT1)
1975 mcr |= UART_MCR_OUT1;
1976 if (mctrl & TIOCM_OUT2)
1977 mcr |= UART_MCR_OUT2;
1978 if (mctrl & TIOCM_LOOP)
1979 mcr |= UART_MCR_LOOP;
1981 mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1983 serial8250_out_MCR(up, mcr);
1985 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
1987 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1989 if (port->set_mctrl)
1990 port->set_mctrl(port, mctrl);
1992 serial8250_do_set_mctrl(port, mctrl);
1995 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1997 struct uart_8250_port *up = up_to_u8250p(port);
1998 unsigned long flags;
2000 serial8250_rpm_get(up);
2001 spin_lock_irqsave(&port->lock, flags);
2002 if (break_state == -1)
2003 up->lcr |= UART_LCR_SBC;
2005 up->lcr &= ~UART_LCR_SBC;
2006 serial_port_out(port, UART_LCR, up->lcr);
2007 spin_unlock_irqrestore(&port->lock, flags);
2008 serial8250_rpm_put(up);
2012 * Wait for transmitter & holding register to empty
2014 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2016 unsigned int status, tmout = 10000;
2018 /* Wait up to 10ms for the character(s) to be sent. */
2020 status = serial_in(up, UART_LSR);
2022 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
2024 if ((status & bits) == bits)
2029 touch_nmi_watchdog();
2032 /* Wait up to 1s for flow control if necessary */
2033 if (up->port.flags & UPF_CONS_FLOW) {
2034 for (tmout = 1000000; tmout; tmout--) {
2035 unsigned int msr = serial_in(up, UART_MSR);
2036 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2037 if (msr & UART_MSR_CTS)
2040 touch_nmi_watchdog();
2045 #ifdef CONFIG_CONSOLE_POLL
2047 * Console polling routines for writing and reading from the uart while
2048 * in an interrupt or debug context.
2051 static int serial8250_get_poll_char(struct uart_port *port)
2053 struct uart_8250_port *up = up_to_u8250p(port);
2057 serial8250_rpm_get(up);
2059 lsr = serial_port_in(port, UART_LSR);
2061 if (!(lsr & UART_LSR_DR)) {
2062 status = NO_POLL_CHAR;
2066 status = serial_port_in(port, UART_RX);
2068 serial8250_rpm_put(up);
2073 static void serial8250_put_poll_char(struct uart_port *port,
2077 struct uart_8250_port *up = up_to_u8250p(port);
2079 serial8250_rpm_get(up);
2081 * First save the IER then disable the interrupts
2083 ier = serial_port_in(port, UART_IER);
2084 if (up->capabilities & UART_CAP_UUE)
2085 serial_port_out(port, UART_IER, UART_IER_UUE);
2087 serial_port_out(port, UART_IER, 0);
2089 wait_for_xmitr(up, BOTH_EMPTY);
2091 * Send the character out.
2093 serial_port_out(port, UART_TX, c);
2096 * Finally, wait for transmitter to become empty
2097 * and restore the IER
2099 wait_for_xmitr(up, BOTH_EMPTY);
2100 serial_port_out(port, UART_IER, ier);
2101 serial8250_rpm_put(up);
2104 #endif /* CONFIG_CONSOLE_POLL */
2106 int serial8250_do_startup(struct uart_port *port)
2108 struct uart_8250_port *up = up_to_u8250p(port);
2109 unsigned long flags;
2110 unsigned char lsr, iir;
2113 if (!port->fifosize)
2114 port->fifosize = uart_config[port->type].fifo_size;
2116 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2117 if (!up->capabilities)
2118 up->capabilities = uart_config[port->type].flags;
2121 if (port->iotype != up->cur_iotype)
2122 set_io_from_upio(port);
2124 serial8250_rpm_get(up);
2125 if (port->type == PORT_16C950) {
2126 /* Wake up and initialize UART */
2128 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2129 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2130 serial_port_out(port, UART_IER, 0);
2131 serial_port_out(port, UART_LCR, 0);
2132 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2133 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2134 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2135 serial_port_out(port, UART_LCR, 0);
2138 #ifdef CONFIG_SERIAL_8250_RSA
2140 * If this is an RSA port, see if we can kick it up to the
2141 * higher speed clock.
2146 if (port->type == PORT_XR17V35X) {
2148 * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
2149 * MCR [7:5] and MSR [7:0]
2151 serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
2154 * Make sure all interrups are masked until initialization is
2155 * complete and the FIFOs are cleared
2157 serial_port_out(port, UART_IER, 0);
2161 * Clear the FIFO buffers and disable them.
2162 * (they will be reenabled in set_termios())
2164 serial8250_clear_fifos(up);
2167 * Clear the interrupt registers.
2169 serial_port_in(port, UART_LSR);
2170 serial_port_in(port, UART_RX);
2171 serial_port_in(port, UART_IIR);
2172 serial_port_in(port, UART_MSR);
2175 * At this point, there's no way the LSR could still be 0xff;
2176 * if it is, then bail out, because there's likely no UART
2179 if (!(port->flags & UPF_BUGGY_UART) &&
2180 (serial_port_in(port, UART_LSR) == 0xff)) {
2181 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2182 serial_index(port));
2188 * For a XR16C850, we need to set the trigger levels
2190 if (port->type == PORT_16850) {
2193 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2195 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2196 serial_port_out(port, UART_FCTR,
2197 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2198 serial_port_out(port, UART_TRG, UART_TRG_96);
2199 serial_port_out(port, UART_FCTR,
2200 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2201 serial_port_out(port, UART_TRG, UART_TRG_96);
2203 serial_port_out(port, UART_LCR, 0);
2207 * For the Altera 16550 variants, set TX threshold trigger level.
2209 if (((port->type == PORT_ALTR_16550_F32) ||
2210 (port->type == PORT_ALTR_16550_F64) ||
2211 (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2212 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2213 if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2214 pr_err("ttyS%d TX FIFO Threshold errors, skipping\n",
2215 serial_index(port));
2217 serial_port_out(port, UART_ALTR_AFR,
2218 UART_ALTR_EN_TXFIFO_LW);
2219 serial_port_out(port, UART_ALTR_TX_LOW,
2220 port->fifosize - up->tx_loadsz);
2221 port->handle_irq = serial8250_tx_threshold_handle_irq;
2225 /* Check if we need to have shared IRQs */
2226 if (port->irq && (up->port.flags & UPF_SHARE_IRQ))
2227 up->port.irqflags |= IRQF_SHARED;
2232 if (port->irqflags & IRQF_SHARED)
2233 disable_irq_nosync(port->irq);
2236 * Test for UARTs that do not reassert THRE when the
2237 * transmitter is idle and the interrupt has already
2238 * been cleared. Real 16550s should always reassert
2239 * this interrupt whenever the transmitter is idle and
2240 * the interrupt is enabled. Delays are necessary to
2241 * allow register changes to become visible.
2243 spin_lock_irqsave(&port->lock, flags);
2245 wait_for_xmitr(up, UART_LSR_THRE);
2246 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2247 udelay(1); /* allow THRE to set */
2248 iir1 = serial_port_in(port, UART_IIR);
2249 serial_port_out(port, UART_IER, 0);
2250 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2251 udelay(1); /* allow a working UART time to re-assert THRE */
2252 iir = serial_port_in(port, UART_IIR);
2253 serial_port_out(port, UART_IER, 0);
2255 spin_unlock_irqrestore(&port->lock, flags);
2257 if (port->irqflags & IRQF_SHARED)
2258 enable_irq(port->irq);
2261 * If the interrupt is not reasserted, or we otherwise
2262 * don't trust the iir, setup a timer to kick the UART
2263 * on a regular basis.
2265 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2266 up->port.flags & UPF_BUG_THRE) {
2267 up->bugs |= UART_BUG_THRE;
2271 retval = up->ops->setup_irq(up);
2276 * Now, initialize the UART
2278 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2280 spin_lock_irqsave(&port->lock, flags);
2281 if (up->port.flags & UPF_FOURPORT) {
2283 up->port.mctrl |= TIOCM_OUT1;
2286 * Most PC uarts need OUT2 raised to enable interrupts.
2289 up->port.mctrl |= TIOCM_OUT2;
2291 serial8250_set_mctrl(port, port->mctrl);
2294 * Serial over Lan (SoL) hack:
2295 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be
2296 * used for Serial Over Lan. Those chips take a longer time than a
2297 * normal serial device to signalize that a transmission data was
2298 * queued. Due to that, the above test generally fails. One solution
2299 * would be to delay the reading of iir. However, this is not
2300 * reliable, since the timeout is variable. So, let's just don't
2301 * test if we receive TX irq. This way, we'll never enable
2304 if (up->port.flags & UPF_NO_TXEN_TEST)
2305 goto dont_test_tx_en;
2308 * Do a quick test to see if we receive an interrupt when we enable
2311 serial_port_out(port, UART_IER, UART_IER_THRI);
2312 lsr = serial_port_in(port, UART_LSR);
2313 iir = serial_port_in(port, UART_IIR);
2314 serial_port_out(port, UART_IER, 0);
2316 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2317 if (!(up->bugs & UART_BUG_TXEN)) {
2318 up->bugs |= UART_BUG_TXEN;
2319 pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2320 serial_index(port));
2323 up->bugs &= ~UART_BUG_TXEN;
2327 spin_unlock_irqrestore(&port->lock, flags);
2330 * Clear the interrupt registers again for luck, and clear the
2331 * saved flags to avoid getting false values from polling
2332 * routines or the previous session.
2334 serial_port_in(port, UART_LSR);
2335 serial_port_in(port, UART_RX);
2336 serial_port_in(port, UART_IIR);
2337 serial_port_in(port, UART_MSR);
2338 up->lsr_saved_flags = 0;
2339 up->msr_saved_flags = 0;
2342 * Request DMA channels for both RX and TX.
2345 retval = serial8250_request_dma(up);
2347 pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2348 serial_index(port));
2354 * Set the IER shadow for rx interrupts but defer actual interrupt
2355 * enable until after the FIFOs are enabled; otherwise, an already-
2356 * active sender can swamp the interrupt handler with "too much work".
2358 up->ier = UART_IER_RLSI | UART_IER_RDI;
2360 if (port->flags & UPF_FOURPORT) {
2363 * Enable interrupts on the AST Fourport board
2365 icp = (port->iobase & 0xfe0) | 0x01f;
2371 serial8250_rpm_put(up);
2374 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2376 static int serial8250_startup(struct uart_port *port)
2379 return port->startup(port);
2380 return serial8250_do_startup(port);
2383 void serial8250_do_shutdown(struct uart_port *port)
2385 struct uart_8250_port *up = up_to_u8250p(port);
2386 unsigned long flags;
2388 serial8250_rpm_get(up);
2390 * Disable interrupts from this port
2392 spin_lock_irqsave(&port->lock, flags);
2394 serial_port_out(port, UART_IER, 0);
2395 spin_unlock_irqrestore(&port->lock, flags);
2397 synchronize_irq(port->irq);
2400 serial8250_release_dma(up);
2402 spin_lock_irqsave(&port->lock, flags);
2403 if (port->flags & UPF_FOURPORT) {
2404 /* reset interrupts on the AST Fourport board */
2405 inb((port->iobase & 0xfe0) | 0x1f);
2406 port->mctrl |= TIOCM_OUT1;
2408 port->mctrl &= ~TIOCM_OUT2;
2410 serial8250_set_mctrl(port, port->mctrl);
2411 spin_unlock_irqrestore(&port->lock, flags);
2414 * Disable break condition and FIFOs
2416 serial_port_out(port, UART_LCR,
2417 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2418 serial8250_clear_fifos(up);
2420 #ifdef CONFIG_SERIAL_8250_RSA
2422 * Reset the RSA board back to 115kbps compat mode.
2428 * Read data port to reset things, and then unlink from
2431 serial_port_in(port, UART_RX);
2432 serial8250_rpm_put(up);
2434 up->ops->release_irq(up);
2436 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2438 static void serial8250_shutdown(struct uart_port *port)
2441 port->shutdown(port);
2443 serial8250_do_shutdown(port);
2447 * XR17V35x UARTs have an extra fractional divisor register (DLD)
2448 * Calculate divisor with extra 4-bit fractional portion
2450 static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2454 struct uart_port *port = &up->port;
2455 unsigned int quot_16;
2457 quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2458 *frac = quot_16 & 0x0f;
2460 return quot_16 >> 4;
2463 static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2467 struct uart_port *port = &up->port;
2471 * Handle magic divisors for baud rates above baud_base on
2472 * SMSC SuperIO chips.
2475 if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2476 baud == (port->uartclk/4))
2478 else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2479 baud == (port->uartclk/8))
2481 else if (up->port.type == PORT_XR17V35X)
2482 quot = xr17v35x_get_divisor(up, baud, frac);
2484 quot = uart_get_divisor(port, baud);
2487 * Oxford Semi 952 rev B workaround
2489 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2495 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2500 switch (c_cflag & CSIZE) {
2502 cval = UART_LCR_WLEN5;
2505 cval = UART_LCR_WLEN6;
2508 cval = UART_LCR_WLEN7;
2512 cval = UART_LCR_WLEN8;
2516 if (c_cflag & CSTOPB)
2517 cval |= UART_LCR_STOP;
2518 if (c_cflag & PARENB) {
2519 cval |= UART_LCR_PARITY;
2520 if (up->bugs & UART_BUG_PARITY)
2521 up->fifo_bug = true;
2523 if (!(c_cflag & PARODD))
2524 cval |= UART_LCR_EPAR;
2526 if (c_cflag & CMSPAR)
2527 cval |= UART_LCR_SPAR;
2533 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2534 unsigned int quot, unsigned int quot_frac)
2536 struct uart_8250_port *up = up_to_u8250p(port);
2538 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2539 if (is_omap1510_8250(up)) {
2540 if (baud == 115200) {
2542 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2544 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2548 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2549 * otherwise just set DLAB
2551 if (up->capabilities & UART_NATSEMI)
2552 serial_port_out(port, UART_LCR, 0xe0);
2554 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2556 serial_dl_write(up, quot);
2558 /* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2559 if (up->port.type == PORT_XR17V35X) {
2560 /* Preserve bits not related to baudrate; DLD[7:4]. */
2561 quot_frac |= serial_port_in(port, 0x2) & 0xf0;
2562 serial_port_out(port, 0x2, quot_frac);
2566 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2567 struct ktermios *termios,
2568 struct ktermios *old)
2571 * Ask the core to calculate the divisor for us.
2572 * Allow 1% tolerance at the upper limit so uart clks marginally
2573 * slower than nominal still match standard baud rates without
2574 * causing transmission errors.
2576 return uart_get_baud_rate(port, termios, old,
2577 port->uartclk / 16 / 0xffff,
2582 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2583 struct ktermios *old)
2585 struct uart_8250_port *up = up_to_u8250p(port);
2587 unsigned long flags;
2588 unsigned int baud, quot, frac = 0;
2590 cval = serial8250_compute_lcr(up, termios->c_cflag);
2592 baud = serial8250_get_baud_rate(port, termios, old);
2593 quot = serial8250_get_divisor(up, baud, &frac);
2596 * Ok, we're now changing the port state. Do it with
2597 * interrupts disabled.
2599 serial8250_rpm_get(up);
2600 spin_lock_irqsave(&port->lock, flags);
2602 up->lcr = cval; /* Save computed LCR */
2604 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2605 /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2606 if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2607 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2608 up->fcr |= UART_FCR_TRIGGER_1;
2613 * MCR-based auto flow control. When AFE is enabled, RTS will be
2614 * deasserted when the receive FIFO contains more characters than
2615 * the trigger, or the MCR RTS bit is cleared.
2617 if (up->capabilities & UART_CAP_AFE) {
2618 up->mcr &= ~UART_MCR_AFE;
2619 if (termios->c_cflag & CRTSCTS)
2620 up->mcr |= UART_MCR_AFE;
2624 * Update the per-port timeout.
2626 uart_update_timeout(port, termios->c_cflag, baud);
2628 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2629 if (termios->c_iflag & INPCK)
2630 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2631 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2632 port->read_status_mask |= UART_LSR_BI;
2635 * Characteres to ignore
2637 port->ignore_status_mask = 0;
2638 if (termios->c_iflag & IGNPAR)
2639 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2640 if (termios->c_iflag & IGNBRK) {
2641 port->ignore_status_mask |= UART_LSR_BI;
2643 * If we're ignoring parity and break indicators,
2644 * ignore overruns too (for real raw support).
2646 if (termios->c_iflag & IGNPAR)
2647 port->ignore_status_mask |= UART_LSR_OE;
2651 * ignore all characters if CREAD is not set
2653 if ((termios->c_cflag & CREAD) == 0)
2654 port->ignore_status_mask |= UART_LSR_DR;
2657 * CTS flow control flag and modem status interrupts
2659 up->ier &= ~UART_IER_MSI;
2660 if (!(up->bugs & UART_BUG_NOMSR) &&
2661 UART_ENABLE_MS(&up->port, termios->c_cflag))
2662 up->ier |= UART_IER_MSI;
2663 if (up->capabilities & UART_CAP_UUE)
2664 up->ier |= UART_IER_UUE;
2665 if (up->capabilities & UART_CAP_RTOIE)
2666 up->ier |= UART_IER_RTOIE;
2668 serial_port_out(port, UART_IER, up->ier);
2670 if (up->capabilities & UART_CAP_EFR) {
2671 unsigned char efr = 0;
2673 * TI16C752/Startech hardware flow control. FIXME:
2674 * - TI16C752 requires control thresholds to be set.
2675 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2677 if (termios->c_cflag & CRTSCTS)
2678 efr |= UART_EFR_CTS;
2680 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2681 if (port->flags & UPF_EXAR_EFR)
2682 serial_port_out(port, UART_XR_EFR, efr);
2684 serial_port_out(port, UART_EFR, efr);
2687 serial8250_set_divisor(port, baud, quot, frac);
2690 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2691 * is written without DLAB set, this mode will be disabled.
2693 if (port->type == PORT_16750)
2694 serial_port_out(port, UART_FCR, up->fcr);
2696 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
2697 if (port->type != PORT_16750) {
2698 /* emulated UARTs (Lucent Venus 167x) need two steps */
2699 if (up->fcr & UART_FCR_ENABLE_FIFO)
2700 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2701 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
2703 serial8250_set_mctrl(port, port->mctrl);
2704 spin_unlock_irqrestore(&port->lock, flags);
2705 serial8250_rpm_put(up);
2707 /* Don't rewrite B0 */
2708 if (tty_termios_baud_rate(termios))
2709 tty_termios_encode_baud_rate(termios, baud, baud);
2711 EXPORT_SYMBOL(serial8250_do_set_termios);
2714 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2715 struct ktermios *old)
2717 if (port->set_termios)
2718 port->set_termios(port, termios, old);
2720 serial8250_do_set_termios(port, termios, old);
2724 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2726 if (termios->c_line == N_PPS) {
2727 port->flags |= UPF_HARDPPS_CD;
2728 spin_lock_irq(&port->lock);
2729 serial8250_enable_ms(port);
2730 spin_unlock_irq(&port->lock);
2732 port->flags &= ~UPF_HARDPPS_CD;
2733 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2734 spin_lock_irq(&port->lock);
2735 serial8250_disable_ms(port);
2736 spin_unlock_irq(&port->lock);
2742 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2743 unsigned int oldstate)
2745 struct uart_8250_port *p = up_to_u8250p(port);
2747 serial8250_set_sleep(p, state != 0);
2749 EXPORT_SYMBOL(serial8250_do_pm);
2752 serial8250_pm(struct uart_port *port, unsigned int state,
2753 unsigned int oldstate)
2756 port->pm(port, state, oldstate);
2758 serial8250_do_pm(port, state, oldstate);
2761 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2763 if (pt->port.mapsize)
2764 return pt->port.mapsize;
2765 if (pt->port.iotype == UPIO_AU) {
2766 if (pt->port.type == PORT_RT2880)
2770 if (is_omap1_8250(pt))
2771 return 0x16 << pt->port.regshift;
2773 return 8 << pt->port.regshift;
2777 * Resource handling.
2779 static int serial8250_request_std_resource(struct uart_8250_port *up)
2781 unsigned int size = serial8250_port_size(up);
2782 struct uart_port *port = &up->port;
2785 switch (port->iotype) {
2792 if (!port->mapbase) {
2797 if (!request_mem_region(port->mapbase, size, "serial")) {
2802 if (port->flags & UPF_IOREMAP) {
2803 port->membase = ioremap_nocache(port->mapbase, size);
2804 if (!port->membase) {
2805 release_mem_region(port->mapbase, size);
2813 if (!request_region(port->iobase, size, "serial"))
2820 static void serial8250_release_std_resource(struct uart_8250_port *up)
2822 unsigned int size = serial8250_port_size(up);
2823 struct uart_port *port = &up->port;
2825 switch (port->iotype) {
2835 if (port->flags & UPF_IOREMAP) {
2836 iounmap(port->membase);
2837 port->membase = NULL;
2840 release_mem_region(port->mapbase, size);
2845 release_region(port->iobase, size);
2850 static void serial8250_release_port(struct uart_port *port)
2852 struct uart_8250_port *up = up_to_u8250p(port);
2854 serial8250_release_std_resource(up);
2857 static int serial8250_request_port(struct uart_port *port)
2859 struct uart_8250_port *up = up_to_u8250p(port);
2861 return serial8250_request_std_resource(up);
2864 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2866 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2867 unsigned char bytes;
2869 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2871 return bytes ? bytes : -EOPNOTSUPP;
2874 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2876 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2879 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2882 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2883 if (bytes < conf_type->rxtrig_bytes[i])
2884 /* Use the nearest lower value */
2885 return (--i) << UART_FCR_R_TRIG_SHIFT;
2888 return UART_FCR_R_TRIG_11;
2891 static int do_get_rxtrig(struct tty_port *port)
2893 struct uart_state *state = container_of(port, struct uart_state, port);
2894 struct uart_port *uport = state->uart_port;
2895 struct uart_8250_port *up = up_to_u8250p(uport);
2897 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2900 return fcr_get_rxtrig_bytes(up);
2903 static int do_serial8250_get_rxtrig(struct tty_port *port)
2907 mutex_lock(&port->mutex);
2908 rxtrig_bytes = do_get_rxtrig(port);
2909 mutex_unlock(&port->mutex);
2911 return rxtrig_bytes;
2914 static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2915 struct device_attribute *attr, char *buf)
2917 struct tty_port *port = dev_get_drvdata(dev);
2920 rxtrig_bytes = do_serial8250_get_rxtrig(port);
2921 if (rxtrig_bytes < 0)
2922 return rxtrig_bytes;
2924 return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2927 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2929 struct uart_state *state = container_of(port, struct uart_state, port);
2930 struct uart_port *uport = state->uart_port;
2931 struct uart_8250_port *up = up_to_u8250p(uport);
2934 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2938 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2942 serial8250_clear_fifos(up);
2943 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2944 up->fcr |= (unsigned char)rxtrig;
2945 serial_out(up, UART_FCR, up->fcr);
2949 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2953 mutex_lock(&port->mutex);
2954 ret = do_set_rxtrig(port, bytes);
2955 mutex_unlock(&port->mutex);
2960 static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2961 struct device_attribute *attr, const char *buf, size_t count)
2963 struct tty_port *port = dev_get_drvdata(dev);
2964 unsigned char bytes;
2970 ret = kstrtou8(buf, 10, &bytes);
2974 ret = do_serial8250_set_rxtrig(port, bytes);
2981 static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
2982 serial8250_get_attr_rx_trig_bytes,
2983 serial8250_set_attr_rx_trig_bytes);
2985 static struct attribute *serial8250_dev_attrs[] = {
2986 &dev_attr_rx_trig_bytes.attr,
2990 static struct attribute_group serial8250_dev_attr_group = {
2991 .attrs = serial8250_dev_attrs,
2994 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
2996 const struct serial8250_config *conf_type = &uart_config[up->port.type];
2998 if (conf_type->rxtrig_bytes[0])
2999 up->port.attr_group = &serial8250_dev_attr_group;
3002 static void serial8250_config_port(struct uart_port *port, int flags)
3004 struct uart_8250_port *up = up_to_u8250p(port);
3008 * Find the region that we can probe for. This in turn
3009 * tells us whether we can probe for the type of port.
3011 ret = serial8250_request_std_resource(up);
3015 if (port->iotype != up->cur_iotype)
3016 set_io_from_upio(port);
3018 if (flags & UART_CONFIG_TYPE)
3021 /* if access method is AU, it is a 16550 with a quirk */
3022 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3023 up->bugs |= UART_BUG_NOMSR;
3025 /* HW bugs may trigger IRQ while IIR == NO_INT */
3026 if (port->type == PORT_TEGRA)
3027 up->bugs |= UART_BUG_NOMSR;
3029 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3032 if (port->type == PORT_UNKNOWN)
3033 serial8250_release_std_resource(up);
3035 /* Fixme: probably not the best place for this */
3036 if ((port->type == PORT_XR17V35X) ||
3037 (port->type == PORT_XR17D15X))
3038 port->handle_irq = exar_handle_irq;
3040 register_dev_spec_attr_grp(up);
3041 up->fcr = uart_config[up->port.type].fcr;
3045 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3047 if (ser->irq >= nr_irqs || ser->irq < 0 ||
3048 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3049 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3050 ser->type == PORT_STARTECH)
3055 static const char *serial8250_type(struct uart_port *port)
3057 int type = port->type;
3059 if (type >= ARRAY_SIZE(uart_config))
3061 return uart_config[type].name;
3064 static const struct uart_ops serial8250_pops = {
3065 .tx_empty = serial8250_tx_empty,
3066 .set_mctrl = serial8250_set_mctrl,
3067 .get_mctrl = serial8250_get_mctrl,
3068 .stop_tx = serial8250_stop_tx,
3069 .start_tx = serial8250_start_tx,
3070 .throttle = serial8250_throttle,
3071 .unthrottle = serial8250_unthrottle,
3072 .stop_rx = serial8250_stop_rx,
3073 .enable_ms = serial8250_enable_ms,
3074 .break_ctl = serial8250_break_ctl,
3075 .startup = serial8250_startup,
3076 .shutdown = serial8250_shutdown,
3077 .set_termios = serial8250_set_termios,
3078 .set_ldisc = serial8250_set_ldisc,
3079 .pm = serial8250_pm,
3080 .type = serial8250_type,
3081 .release_port = serial8250_release_port,
3082 .request_port = serial8250_request_port,
3083 .config_port = serial8250_config_port,
3084 .verify_port = serial8250_verify_port,
3085 #ifdef CONFIG_CONSOLE_POLL
3086 .poll_get_char = serial8250_get_poll_char,
3087 .poll_put_char = serial8250_put_poll_char,
3091 void serial8250_init_port(struct uart_8250_port *up)
3093 struct uart_port *port = &up->port;
3095 spin_lock_init(&port->lock);
3096 port->ops = &serial8250_pops;
3098 up->cur_iotype = 0xFF;
3100 EXPORT_SYMBOL_GPL(serial8250_init_port);
3102 void serial8250_set_defaults(struct uart_8250_port *up)
3104 struct uart_port *port = &up->port;
3106 if (up->port.flags & UPF_FIXED_TYPE) {
3107 unsigned int type = up->port.type;
3109 if (!up->port.fifosize)
3110 up->port.fifosize = uart_config[type].fifo_size;
3112 up->tx_loadsz = uart_config[type].tx_loadsz;
3113 if (!up->capabilities)
3114 up->capabilities = uart_config[type].flags;
3117 set_io_from_upio(port);
3119 /* default dma handlers */
3121 if (!up->dma->tx_dma)
3122 up->dma->tx_dma = serial8250_tx_dma;
3123 if (!up->dma->rx_dma)
3124 up->dma->rx_dma = serial8250_rx_dma;
3127 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3129 #ifdef CONFIG_SERIAL_8250_CONSOLE
3131 static void serial8250_console_putchar(struct uart_port *port, int ch)
3133 struct uart_8250_port *up = up_to_u8250p(port);
3135 wait_for_xmitr(up, UART_LSR_THRE);
3136 serial_port_out(port, UART_TX, ch);
3140 * Restore serial console when h/w power-off detected
3142 static void serial8250_console_restore(struct uart_8250_port *up)
3144 struct uart_port *port = &up->port;
3145 struct ktermios termios;
3146 unsigned int baud, quot, frac = 0;
3148 termios.c_cflag = port->cons->cflag;
3149 termios.c_ispeed = port->cons->ispeed;
3150 termios.c_ospeed = port->cons->ospeed;
3151 if (port->state->port.tty && termios.c_cflag == 0) {
3152 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3153 termios.c_ispeed = port->state->port.tty->termios.c_ispeed;
3154 termios.c_ospeed = port->state->port.tty->termios.c_ospeed;
3157 baud = serial8250_get_baud_rate(port, &termios, NULL);
3158 quot = serial8250_get_divisor(up, baud, &frac);
3160 serial8250_set_divisor(port, baud, quot, frac);
3161 serial_port_out(port, UART_LCR, up->lcr);
3162 serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS);
3166 * Print a string to the serial port trying not to disturb
3167 * any possible real use of the port...
3169 * The console_lock must be held when we get here.
3171 void serial8250_console_write(struct uart_8250_port *up, const char *s,
3174 struct uart_port *port = &up->port;
3175 unsigned long flags;
3179 touch_nmi_watchdog();
3181 serial8250_rpm_get(up);
3185 else if (oops_in_progress)
3186 locked = spin_trylock_irqsave(&port->lock, flags);
3188 spin_lock_irqsave(&port->lock, flags);
3191 * First save the IER then disable the interrupts
3193 ier = serial_port_in(port, UART_IER);
3195 if (up->capabilities & UART_CAP_UUE)
3196 serial_port_out(port, UART_IER, UART_IER_UUE);
3198 serial_port_out(port, UART_IER, 0);
3200 /* check scratch reg to see if port powered off during system sleep */
3201 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3202 serial8250_console_restore(up);
3206 uart_console_write(port, s, count, serial8250_console_putchar);
3209 * Finally, wait for transmitter to become empty
3210 * and restore the IER
3212 wait_for_xmitr(up, BOTH_EMPTY);
3213 serial_port_out(port, UART_IER, ier);
3216 * The receive handling will happen properly because the
3217 * receive ready bit will still be set; it is not cleared
3218 * on read. However, modem control will not, we must
3219 * call it if we have saved something in the saved flags
3220 * while processing with interrupts off.
3222 if (up->msr_saved_flags)
3223 serial8250_modem_status(up);
3226 spin_unlock_irqrestore(&port->lock, flags);
3227 serial8250_rpm_put(up);
3230 static unsigned int probe_baud(struct uart_port *port)
3232 unsigned char lcr, dll, dlm;
3235 lcr = serial_port_in(port, UART_LCR);
3236 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3237 dll = serial_port_in(port, UART_DLL);
3238 dlm = serial_port_in(port, UART_DLM);
3239 serial_port_out(port, UART_LCR, lcr);
3241 quot = (dlm << 8) | dll;
3242 return (port->uartclk / 16) / quot;
3245 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3252 if (!port->iobase && !port->membase)
3256 uart_parse_options(options, &baud, &parity, &bits, &flow);
3258 baud = probe_baud(port);
3260 return uart_set_options(port, port->cons, baud, parity, bits, flow);
3263 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3265 MODULE_LICENSE("GPL");