1 // SPDX-License-Identifier: GPL-2.0+
3 * Base port operations for 8250/16550-type serial ports
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 * Split from 8250_core.c, Copyright (C) 2001 Russell King.
8 * A note about mapbase / membase
10 * mapbase is the physical address of the IO port.
11 * membase is an 'ioremapped' cookie.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/irq.h>
19 #include <linux/console.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/sysrq.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/tty.h>
25 #include <linux/ratelimit.h>
26 #include <linux/tty_flip.h>
27 #include <linux/serial.h>
28 #include <linux/serial_8250.h>
29 #include <linux/nmi.h>
30 #include <linux/mutex.h>
31 #include <linux/slab.h>
32 #include <linux/uaccess.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/ktime.h>
41 /* Nuvoton NPCM timeout register */
42 #define UART_NPCM_TOR 7
43 #define UART_NPCM_TOIE BIT(7) /* Timeout Interrupt Enable */
49 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
51 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
55 * Here we define the default xmit fifo size used for each type of UART.
57 static const struct serial8250_config uart_config[] = {
82 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
83 .rxtrig_bytes = {1, 4, 8, 14},
84 .flags = UART_CAP_FIFO,
95 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
101 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
103 .rxtrig_bytes = {8, 16, 24, 28},
104 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
110 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
112 .rxtrig_bytes = {1, 16, 32, 56},
113 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
121 .name = "16C950/954",
124 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
125 .rxtrig_bytes = {16, 32, 112, 120},
126 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
127 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
133 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
135 .rxtrig_bytes = {8, 16, 56, 60},
136 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
142 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
143 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
149 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
150 .flags = UART_CAP_FIFO,
156 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
157 .flags = UART_CAP_FIFO | UART_NATSEMI,
163 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
164 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
170 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
171 .flags = UART_CAP_FIFO,
177 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
178 .flags = UART_CAP_FIFO /* | UART_CAP_AFE */,
184 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
185 .flags = UART_CAP_FIFO | UART_CAP_AFE,
191 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
193 .rxtrig_bytes = {1, 4, 8, 14},
194 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
200 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
201 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
208 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
210 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
217 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
218 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
219 .flags = UART_CAP_FIFO,
221 [PORT_BRCM_TRUMANAGE] = {
225 .flags = UART_CAP_HFIFO,
230 [PORT_ALTR_16550_F32] = {
231 .name = "Altera 16550 FIFO32",
234 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
235 .rxtrig_bytes = {1, 8, 16, 30},
236 .flags = UART_CAP_FIFO | UART_CAP_AFE,
238 [PORT_ALTR_16550_F64] = {
239 .name = "Altera 16550 FIFO64",
242 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
243 .rxtrig_bytes = {1, 16, 32, 62},
244 .flags = UART_CAP_FIFO | UART_CAP_AFE,
246 [PORT_ALTR_16550_F128] = {
247 .name = "Altera 16550 FIFO128",
250 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
251 .rxtrig_bytes = {1, 32, 64, 126},
252 .flags = UART_CAP_FIFO | UART_CAP_AFE,
255 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement
256 * workaround of errata A-008006 which states that tx_loadsz should
257 * be configured less than Maximum supported fifo bytes.
259 [PORT_16550A_FSL64] = {
260 .name = "16550A_FSL64",
263 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
265 .flags = UART_CAP_FIFO | UART_CAP_NOTEMT,
268 .name = "Palmchip BK-3103",
271 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
272 .rxtrig_bytes = {1, 4, 8, 14},
273 .flags = UART_CAP_FIFO,
276 .name = "TI DA8xx/66AK2x",
279 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
281 .rxtrig_bytes = {1, 4, 8, 14},
282 .flags = UART_CAP_FIFO | UART_CAP_AFE,
285 .name = "MediaTek BTIF",
288 .fcr = UART_FCR_ENABLE_FIFO |
289 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
290 .flags = UART_CAP_FIFO,
293 .name = "Nuvoton 16550",
296 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
297 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
298 .rxtrig_bytes = {1, 4, 8, 14},
299 .flags = UART_CAP_FIFO,
305 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
306 .rxtrig_bytes = {1, 32, 64, 112},
307 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
309 [PORT_ASPEED_VUART] = {
310 .name = "ASPEED VUART",
313 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
314 .rxtrig_bytes = {1, 4, 8, 14},
315 .flags = UART_CAP_FIFO,
317 [PORT_MCHP16550A] = {
318 .name = "MCHP16550A",
321 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
322 .rxtrig_bytes = {2, 66, 130, 194},
323 .flags = UART_CAP_FIFO,
327 /* Uart divisor latch read */
328 static u32 default_serial_dl_read(struct uart_8250_port *up)
330 /* Assign these in pieces to truncate any bits above 7. */
331 unsigned char dll = serial_in(up, UART_DLL);
332 unsigned char dlm = serial_in(up, UART_DLM);
334 return dll | dlm << 8;
337 /* Uart divisor latch write */
338 static void default_serial_dl_write(struct uart_8250_port *up, u32 value)
340 serial_out(up, UART_DLL, value & 0xff);
341 serial_out(up, UART_DLM, value >> 8 & 0xff);
344 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
346 offset = offset << p->regshift;
347 outb(p->hub6 - 1 + offset, p->iobase);
348 return inb(p->iobase + 1);
351 static void hub6_serial_out(struct uart_port *p, int offset, int value)
353 offset = offset << p->regshift;
354 outb(p->hub6 - 1 + offset, p->iobase);
355 outb(value, p->iobase + 1);
358 static unsigned int mem_serial_in(struct uart_port *p, int offset)
360 offset = offset << p->regshift;
361 return readb(p->membase + offset);
364 static void mem_serial_out(struct uart_port *p, int offset, int value)
366 offset = offset << p->regshift;
367 writeb(value, p->membase + offset);
370 static void mem16_serial_out(struct uart_port *p, int offset, int value)
372 offset = offset << p->regshift;
373 writew(value, p->membase + offset);
376 static unsigned int mem16_serial_in(struct uart_port *p, int offset)
378 offset = offset << p->regshift;
379 return readw(p->membase + offset);
382 static void mem32_serial_out(struct uart_port *p, int offset, int value)
384 offset = offset << p->regshift;
385 writel(value, p->membase + offset);
388 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
390 offset = offset << p->regshift;
391 return readl(p->membase + offset);
394 static void mem32be_serial_out(struct uart_port *p, int offset, int value)
396 offset = offset << p->regshift;
397 iowrite32be(value, p->membase + offset);
400 static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
402 offset = offset << p->regshift;
403 return ioread32be(p->membase + offset);
406 static unsigned int io_serial_in(struct uart_port *p, int offset)
408 offset = offset << p->regshift;
409 return inb(p->iobase + offset);
412 static void io_serial_out(struct uart_port *p, int offset, int value)
414 offset = offset << p->regshift;
415 outb(value, p->iobase + offset);
418 static int serial8250_default_handle_irq(struct uart_port *port);
420 static void set_io_from_upio(struct uart_port *p)
422 struct uart_8250_port *up = up_to_u8250p(p);
424 up->dl_read = default_serial_dl_read;
425 up->dl_write = default_serial_dl_write;
429 p->serial_in = hub6_serial_in;
430 p->serial_out = hub6_serial_out;
434 p->serial_in = mem_serial_in;
435 p->serial_out = mem_serial_out;
439 p->serial_in = mem16_serial_in;
440 p->serial_out = mem16_serial_out;
444 p->serial_in = mem32_serial_in;
445 p->serial_out = mem32_serial_out;
449 p->serial_in = mem32be_serial_in;
450 p->serial_out = mem32be_serial_out;
454 p->serial_in = io_serial_in;
455 p->serial_out = io_serial_out;
458 /* Remember loaded iotype */
459 up->cur_iotype = p->iotype;
460 p->handle_irq = serial8250_default_handle_irq;
464 serial_port_out_sync(struct uart_port *p, int offset, int value)
472 p->serial_out(p, offset, value);
473 p->serial_in(p, UART_LCR); /* safe, no side-effects */
476 p->serial_out(p, offset, value);
483 static void serial8250_clear_fifos(struct uart_8250_port *p)
485 if (p->capabilities & UART_CAP_FIFO) {
486 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
487 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
488 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
489 serial_out(p, UART_FCR, 0);
493 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t);
494 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t);
496 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
498 serial8250_clear_fifos(p);
499 serial_out(p, UART_FCR, p->fcr);
501 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
503 void serial8250_rpm_get(struct uart_8250_port *p)
505 if (!(p->capabilities & UART_CAP_RPM))
507 pm_runtime_get_sync(p->port.dev);
509 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
511 void serial8250_rpm_put(struct uart_8250_port *p)
513 if (!(p->capabilities & UART_CAP_RPM))
515 pm_runtime_mark_last_busy(p->port.dev);
516 pm_runtime_put_autosuspend(p->port.dev);
518 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
521 * serial8250_em485_init() - put uart_8250_port into rs485 emulating
522 * @p: uart_8250_port port instance
524 * The function is used to start rs485 software emulating on the
525 * &struct uart_8250_port* @p. Namely, RTS is switched before/after
526 * transmission. The function is idempotent, so it is safe to call it
529 * The caller MUST enable interrupt on empty shift register before
530 * calling serial8250_em485_init(). This interrupt is not a part of
531 * 8250 standard, but implementation defined.
533 * The function is supposed to be called from .rs485_config callback
534 * or from any other callback protected with p->port.lock spinlock.
536 * See also serial8250_em485_destroy()
538 * Return 0 - success, -errno - otherwise
540 static int serial8250_em485_init(struct uart_8250_port *p)
542 /* Port locked to synchronize UART_IER access against the console. */
543 lockdep_assert_held_once(&p->port.lock);
548 p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC);
552 hrtimer_init(&p->em485->stop_tx_timer, CLOCK_MONOTONIC,
554 hrtimer_init(&p->em485->start_tx_timer, CLOCK_MONOTONIC,
556 p->em485->stop_tx_timer.function = &serial8250_em485_handle_stop_tx;
557 p->em485->start_tx_timer.function = &serial8250_em485_handle_start_tx;
559 p->em485->active_timer = NULL;
560 p->em485->tx_stopped = true;
563 if (p->em485->tx_stopped)
570 * serial8250_em485_destroy() - put uart_8250_port into normal state
571 * @p: uart_8250_port port instance
573 * The function is used to stop rs485 software emulating on the
574 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to
575 * call it multiple times.
577 * The function is supposed to be called from .rs485_config callback
578 * or from any other callback protected with p->port.lock spinlock.
580 * See also serial8250_em485_init()
582 void serial8250_em485_destroy(struct uart_8250_port *p)
587 hrtimer_cancel(&p->em485->start_tx_timer);
588 hrtimer_cancel(&p->em485->stop_tx_timer);
593 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
595 struct serial_rs485 serial8250_em485_supported = {
596 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
597 SER_RS485_TERMINATE_BUS | SER_RS485_RX_DURING_TX,
598 .delay_rts_before_send = 1,
599 .delay_rts_after_send = 1,
601 EXPORT_SYMBOL_GPL(serial8250_em485_supported);
604 * serial8250_em485_config() - generic ->rs485_config() callback
606 * @termios: termios structure
607 * @rs485: rs485 settings
609 * Generic callback usable by 8250 uart drivers to activate rs485 settings
610 * if the uart is incapable of driving RTS as a Transmit Enable signal in
611 * hardware, relying on software emulation instead.
613 int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
614 struct serial_rs485 *rs485)
616 struct uart_8250_port *up = up_to_u8250p(port);
618 /* pick sane settings if the user hasn't */
619 if (!!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
620 !!(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
621 rs485->flags |= SER_RS485_RTS_ON_SEND;
622 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
626 * Both serial8250_em485_init() and serial8250_em485_destroy()
629 if (rs485->flags & SER_RS485_ENABLED)
630 return serial8250_em485_init(up);
632 serial8250_em485_destroy(up);
635 EXPORT_SYMBOL_GPL(serial8250_em485_config);
638 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
639 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
640 * empty and the HW can idle again.
642 void serial8250_rpm_get_tx(struct uart_8250_port *p)
644 unsigned char rpm_active;
646 if (!(p->capabilities & UART_CAP_RPM))
649 rpm_active = xchg(&p->rpm_tx_active, 1);
652 pm_runtime_get_sync(p->port.dev);
654 EXPORT_SYMBOL_GPL(serial8250_rpm_get_tx);
656 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);
669 EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx);
672 * IER sleep support. UARTs which have EFRs need the "extended
673 * capability" bit enabled. Note that on XR16C850s, we need to
674 * reset LCR to write to IER.
676 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
678 unsigned char lcr = 0, efr = 0;
680 serial8250_rpm_get(p);
682 if (p->capabilities & UART_CAP_SLEEP) {
683 /* Synchronize UART_IER access against the console. */
684 spin_lock_irq(&p->port.lock);
685 if (p->capabilities & UART_CAP_EFR) {
686 lcr = serial_in(p, UART_LCR);
687 efr = serial_in(p, UART_EFR);
688 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
689 serial_out(p, UART_EFR, UART_EFR_ECB);
690 serial_out(p, UART_LCR, 0);
692 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
693 if (p->capabilities & UART_CAP_EFR) {
694 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
695 serial_out(p, UART_EFR, efr);
696 serial_out(p, UART_LCR, lcr);
698 spin_unlock_irq(&p->port.lock);
701 serial8250_rpm_put(p);
704 static void serial8250_clear_IER(struct uart_8250_port *up)
706 if (up->capabilities & UART_CAP_UUE)
707 serial_out(up, UART_IER, UART_IER_UUE);
709 serial_out(up, UART_IER, 0);
712 #ifdef CONFIG_SERIAL_8250_RSA
714 * Attempts to turn on the RSA FIFO. Returns zero on failure.
715 * We set the port uart clock rate if we succeed.
717 static int __enable_rsa(struct uart_8250_port *up)
722 mode = serial_in(up, UART_RSA_MSR);
723 result = mode & UART_RSA_MSR_FIFO;
726 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
727 mode = serial_in(up, UART_RSA_MSR);
728 result = mode & UART_RSA_MSR_FIFO;
732 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
737 static void enable_rsa(struct uart_8250_port *up)
739 if (up->port.type == PORT_RSA) {
740 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
741 spin_lock_irq(&up->port.lock);
743 spin_unlock_irq(&up->port.lock);
745 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
746 serial_out(up, UART_RSA_FRR, 0);
751 * Attempts to turn off the RSA FIFO. Returns zero on failure.
752 * It is unknown why interrupts were disabled in here. However,
753 * the caller is expected to preserve this behaviour by grabbing
754 * the spinlock before calling this function.
756 static void disable_rsa(struct uart_8250_port *up)
761 if (up->port.type == PORT_RSA &&
762 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
763 spin_lock_irq(&up->port.lock);
765 mode = serial_in(up, UART_RSA_MSR);
766 result = !(mode & UART_RSA_MSR_FIFO);
769 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
770 mode = serial_in(up, UART_RSA_MSR);
771 result = !(mode & UART_RSA_MSR_FIFO);
775 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
776 spin_unlock_irq(&up->port.lock);
779 #endif /* CONFIG_SERIAL_8250_RSA */
782 * This is a quickie test to see how big the FIFO is.
783 * It doesn't work at all the time, more's the pity.
785 static int size_fifo(struct uart_8250_port *up)
787 unsigned char old_fcr, old_mcr, old_lcr;
791 old_lcr = serial_in(up, UART_LCR);
792 serial_out(up, UART_LCR, 0);
793 old_fcr = serial_in(up, UART_FCR);
794 old_mcr = serial8250_in_MCR(up);
795 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
796 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
797 serial8250_out_MCR(up, UART_MCR_LOOP);
798 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
799 old_dl = serial_dl_read(up);
800 serial_dl_write(up, 0x0001);
801 serial_out(up, UART_LCR, UART_LCR_WLEN8);
802 for (count = 0; count < 256; count++)
803 serial_out(up, UART_TX, count);
804 mdelay(20);/* FIXME - schedule_timeout */
805 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
806 (count < 256); count++)
807 serial_in(up, UART_RX);
808 serial_out(up, UART_FCR, old_fcr);
809 serial8250_out_MCR(up, old_mcr);
810 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
811 serial_dl_write(up, old_dl);
812 serial_out(up, UART_LCR, old_lcr);
818 * Read UART ID using the divisor method - set DLL and DLM to zero
819 * and the revision will be in DLL and device type in DLM. We
820 * preserve the device state across this.
822 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
824 unsigned char old_lcr;
825 unsigned int id, old_dl;
827 old_lcr = serial_in(p, UART_LCR);
828 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
829 old_dl = serial_dl_read(p);
830 serial_dl_write(p, 0);
831 id = serial_dl_read(p);
832 serial_dl_write(p, old_dl);
834 serial_out(p, UART_LCR, old_lcr);
840 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
841 * When this function is called we know it is at least a StarTech
842 * 16650 V2, but it might be one of several StarTech UARTs, or one of
843 * its clones. (We treat the broken original StarTech 16650 V1 as a
844 * 16550, and why not? Startech doesn't seem to even acknowledge its
847 * What evil have men's minds wrought...
849 static void autoconfig_has_efr(struct uart_8250_port *up)
851 unsigned int id1, id2, id3, rev;
854 * Everything with an EFR has SLEEP
856 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
859 * First we check to see if it's an Oxford Semiconductor UART.
861 * If we have to do this here because some non-National
862 * Semiconductor clone chips lock up if you try writing to the
863 * LSR register (which serial_icr_read does)
867 * Check for Oxford Semiconductor 16C950.
869 * EFR [4] must be set else this test fails.
871 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
872 * claims that it's needed for 952 dual UART's (which are not
873 * recommended for new designs).
876 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
877 serial_out(up, UART_EFR, UART_EFR_ECB);
878 serial_out(up, UART_LCR, 0x00);
879 id1 = serial_icr_read(up, UART_ID1);
880 id2 = serial_icr_read(up, UART_ID2);
881 id3 = serial_icr_read(up, UART_ID3);
882 rev = serial_icr_read(up, UART_REV);
884 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
886 if (id1 == 0x16 && id2 == 0xC9 &&
887 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
888 up->port.type = PORT_16C950;
891 * Enable work around for the Oxford Semiconductor 952 rev B
892 * chip which causes it to seriously miscalculate baud rates
895 if (id3 == 0x52 && rev == 0x01)
896 up->bugs |= UART_BUG_QUOT;
901 * We check for a XR16C850 by setting DLL and DLM to 0, and then
902 * reading back DLL and DLM. The chip type depends on the DLM
904 * 0x10 - XR16C850 and the DLL contains the chip revision.
908 id1 = autoconfig_read_divisor_id(up);
909 DEBUG_AUTOCONF("850id=%04x ", id1);
912 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
913 up->port.type = PORT_16850;
918 * It wasn't an XR16C850.
920 * We distinguish between the '654 and the '650 by counting
921 * how many bytes are in the FIFO. I'm using this for now,
922 * since that's the technique that was sent to me in the
923 * serial driver update, but I'm not convinced this works.
924 * I've had problems doing this in the past. -TYT
926 if (size_fifo(up) == 64)
927 up->port.type = PORT_16654;
929 up->port.type = PORT_16650V2;
933 * We detected a chip without a FIFO. Only two fall into
934 * this category - the original 8250 and the 16450. The
935 * 16450 has a scratch register (accessible with LCR=0)
937 static void autoconfig_8250(struct uart_8250_port *up)
939 unsigned char scratch, status1, status2;
941 up->port.type = PORT_8250;
943 scratch = serial_in(up, UART_SCR);
944 serial_out(up, UART_SCR, 0xa5);
945 status1 = serial_in(up, UART_SCR);
946 serial_out(up, UART_SCR, 0x5a);
947 status2 = serial_in(up, UART_SCR);
948 serial_out(up, UART_SCR, scratch);
950 if (status1 == 0xa5 && status2 == 0x5a)
951 up->port.type = PORT_16450;
954 static int broken_efr(struct uart_8250_port *up)
957 * Exar ST16C2550 "A2" devices incorrectly detect as
958 * having an EFR, and report an ID of 0x0201. See
959 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
961 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
968 * We know that the chip has FIFOs. Does it have an EFR? The
969 * EFR is located in the same register position as the IIR and
970 * we know the top two bits of the IIR are currently set. The
971 * EFR should contain zero. Try to read the EFR.
973 static void autoconfig_16550a(struct uart_8250_port *up)
975 unsigned char status1, status2;
976 unsigned int iersave;
978 /* Port locked to synchronize UART_IER access against the console. */
979 lockdep_assert_held_once(&up->port.lock);
981 up->port.type = PORT_16550A;
982 up->capabilities |= UART_CAP_FIFO;
984 if (!IS_ENABLED(CONFIG_SERIAL_8250_16550A_VARIANTS) &&
985 !(up->port.flags & UPF_FULL_PROBE))
989 * Check for presence of the EFR when DLAB is set.
990 * Only ST16C650V1 UARTs pass this test.
992 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
993 if (serial_in(up, UART_EFR) == 0) {
994 serial_out(up, UART_EFR, 0xA8);
995 if (serial_in(up, UART_EFR) != 0) {
996 DEBUG_AUTOCONF("EFRv1 ");
997 up->port.type = PORT_16650;
998 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1000 serial_out(up, UART_LCR, 0);
1001 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1003 status1 = serial_in(up, UART_IIR) & (UART_IIR_64BYTE_FIFO |
1004 UART_IIR_FIFO_ENABLED);
1005 serial_out(up, UART_FCR, 0);
1006 serial_out(up, UART_LCR, 0);
1008 if (status1 == (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED))
1009 up->port.type = PORT_16550A_FSL64;
1011 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1013 serial_out(up, UART_EFR, 0);
1018 * Maybe it requires 0xbf to be written to the LCR.
1019 * (other ST16C650V2 UARTs, TI16C752A, etc)
1021 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1022 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1023 DEBUG_AUTOCONF("EFRv2 ");
1024 autoconfig_has_efr(up);
1029 * Check for a National Semiconductor SuperIO chip.
1030 * Attempt to switch to bank 2, read the value of the LOOP bit
1031 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1032 * switch back to bank 2, read it from EXCR1 again and check
1033 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1035 serial_out(up, UART_LCR, 0);
1036 status1 = serial8250_in_MCR(up);
1037 serial_out(up, UART_LCR, 0xE0);
1038 status2 = serial_in(up, 0x02); /* EXCR1 */
1040 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1041 serial_out(up, UART_LCR, 0);
1042 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
1043 serial_out(up, UART_LCR, 0xE0);
1044 status2 = serial_in(up, 0x02); /* EXCR1 */
1045 serial_out(up, UART_LCR, 0);
1046 serial8250_out_MCR(up, status1);
1048 if ((status2 ^ status1) & UART_MCR_LOOP) {
1049 unsigned short quot;
1051 serial_out(up, UART_LCR, 0xE0);
1053 quot = serial_dl_read(up);
1056 if (ns16550a_goto_highspeed(up))
1057 serial_dl_write(up, quot);
1059 serial_out(up, UART_LCR, 0);
1061 up->port.uartclk = 921600*16;
1062 up->port.type = PORT_NS16550A;
1063 up->capabilities |= UART_NATSEMI;
1069 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1070 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1071 * Try setting it with and without DLAB set. Cheap clones
1072 * set bit 5 without DLAB set.
1074 serial_out(up, UART_LCR, 0);
1075 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1076 status1 = serial_in(up, UART_IIR) & (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED);
1077 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1079 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1080 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1081 status2 = serial_in(up, UART_IIR) & (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED);
1082 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1084 serial_out(up, UART_LCR, 0);
1086 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1088 if (status1 == UART_IIR_FIFO_ENABLED_16550A &&
1089 status2 == (UART_IIR_64BYTE_FIFO | UART_IIR_FIFO_ENABLED_16550A)) {
1090 up->port.type = PORT_16750;
1091 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1096 * Try writing and reading the UART_IER_UUE bit (b6).
1097 * If it works, this is probably one of the Xscale platform's
1099 * We're going to explicitly set the UUE bit to 0 before
1100 * trying to write and read a 1 just to make sure it's not
1101 * already a 1 and maybe locked there before we even start.
1103 iersave = serial_in(up, UART_IER);
1104 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1105 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1107 * OK it's in a known zero state, try writing and reading
1108 * without disturbing the current state of the other bits.
1110 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1111 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1114 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1116 DEBUG_AUTOCONF("Xscale ");
1117 up->port.type = PORT_XSCALE;
1118 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1123 * If we got here we couldn't force the IER_UUE bit to 0.
1124 * Log it and continue.
1126 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1128 serial_out(up, UART_IER, iersave);
1131 * We distinguish between 16550A and U6 16550A by counting
1132 * how many bytes are in the FIFO.
1134 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1135 up->port.type = PORT_U6_16550A;
1136 up->capabilities |= UART_CAP_AFE;
1141 * This routine is called by rs_init() to initialize a specific serial
1142 * port. It determines what type of UART chip this serial port is
1143 * using: 8250, 16450, 16550, 16550A. The important question is
1144 * whether or not this UART is a 16550A or not, since this will
1145 * determine whether or not we can use its FIFO features or not.
1147 static void autoconfig(struct uart_8250_port *up)
1149 unsigned char status1, scratch, scratch2, scratch3;
1150 unsigned char save_lcr, save_mcr;
1151 struct uart_port *port = &up->port;
1152 unsigned long flags;
1153 unsigned int old_capabilities;
1155 if (!port->iobase && !port->mapbase && !port->membase)
1158 DEBUG_AUTOCONF("%s: autoconf (0x%04lx, 0x%p): ",
1159 port->name, port->iobase, port->membase);
1162 * We really do need global IRQs disabled here - we're going to
1163 * be frobbing the chips IRQ enable register to see if it exists.
1165 * Synchronize UART_IER access against the console.
1167 spin_lock_irqsave(&port->lock, flags);
1169 up->capabilities = 0;
1172 if (!(port->flags & UPF_BUGGY_UART)) {
1174 * Do a simple existence test first; if we fail this,
1175 * there's no point trying anything else.
1177 * 0x80 is used as a nonsense port to prevent against
1178 * false positives due to ISA bus float. The
1179 * assumption is that 0x80 is a non-existent port;
1180 * which should be safe since include/asm/io.h also
1181 * makes this assumption.
1183 * Note: this is safe as long as MCR bit 4 is clear
1184 * and the device is in "PC" mode.
1186 scratch = serial_in(up, UART_IER);
1187 serial_out(up, UART_IER, 0);
1192 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1193 * 16C754B) allow only to modify them if an EFR bit is set.
1195 scratch2 = serial_in(up, UART_IER) & UART_IER_ALL_INTR;
1196 serial_out(up, UART_IER, UART_IER_ALL_INTR);
1200 scratch3 = serial_in(up, UART_IER) & UART_IER_ALL_INTR;
1201 serial_out(up, UART_IER, scratch);
1202 if (scratch2 != 0 || scratch3 != UART_IER_ALL_INTR) {
1204 * We failed; there's nothing here
1206 spin_unlock_irqrestore(&port->lock, flags);
1207 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1208 scratch2, scratch3);
1213 save_mcr = serial8250_in_MCR(up);
1214 save_lcr = serial_in(up, UART_LCR);
1217 * Check to see if a UART is really there. Certain broken
1218 * internal modems based on the Rockwell chipset fail this
1219 * test, because they apparently don't implement the loopback
1220 * test mode. So this test is skipped on the COM 1 through
1221 * COM 4 ports. This *should* be safe, since no board
1222 * manufacturer would be stupid enough to design a board
1223 * that conflicts with COM 1-4 --- we hope!
1225 if (!(port->flags & UPF_SKIP_TEST)) {
1226 serial8250_out_MCR(up, UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_RTS);
1227 status1 = serial_in(up, UART_MSR) & UART_MSR_STATUS_BITS;
1228 serial8250_out_MCR(up, save_mcr);
1229 if (status1 != (UART_MSR_DCD | UART_MSR_CTS)) {
1230 spin_unlock_irqrestore(&port->lock, flags);
1231 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1238 * We're pretty sure there's a port here. Lets find out what
1239 * type of port it is. The IIR top two bits allows us to find
1240 * out if it's 8250 or 16450, 16550, 16550A or later. This
1241 * determines what we test for next.
1243 * We also initialise the EFR (if any) to zero for later. The
1244 * EFR occupies the same register location as the FCR and IIR.
1246 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1247 serial_out(up, UART_EFR, 0);
1248 serial_out(up, UART_LCR, 0);
1250 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1252 switch (serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED) {
1253 case UART_IIR_FIFO_ENABLED_8250:
1254 autoconfig_8250(up);
1256 case UART_IIR_FIFO_ENABLED_16550:
1257 port->type = PORT_16550;
1259 case UART_IIR_FIFO_ENABLED_16550A:
1260 autoconfig_16550a(up);
1263 port->type = PORT_UNKNOWN;
1267 #ifdef CONFIG_SERIAL_8250_RSA
1269 * Only probe for RSA ports if we got the region.
1271 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1273 port->type = PORT_RSA;
1276 serial_out(up, UART_LCR, save_lcr);
1278 port->fifosize = uart_config[up->port.type].fifo_size;
1279 old_capabilities = up->capabilities;
1280 up->capabilities = uart_config[port->type].flags;
1281 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1283 if (port->type == PORT_UNKNOWN)
1289 #ifdef CONFIG_SERIAL_8250_RSA
1290 if (port->type == PORT_RSA)
1291 serial_out(up, UART_RSA_FRR, 0);
1293 serial8250_out_MCR(up, save_mcr);
1294 serial8250_clear_fifos(up);
1295 serial_in(up, UART_RX);
1296 serial8250_clear_IER(up);
1299 spin_unlock_irqrestore(&port->lock, flags);
1302 * Check if the device is a Fintek F81216A
1304 if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1305 fintek_8250_probe(up);
1307 if (up->capabilities != old_capabilities) {
1308 dev_warn(port->dev, "detected caps %08x should be %08x\n",
1309 old_capabilities, up->capabilities);
1312 DEBUG_AUTOCONF("iir=%d ", scratch);
1313 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1316 static void autoconfig_irq(struct uart_8250_port *up)
1318 struct uart_port *port = &up->port;
1319 unsigned char save_mcr, save_ier;
1320 unsigned char save_ICP = 0;
1321 unsigned int ICP = 0;
1325 if (port->flags & UPF_FOURPORT) {
1326 ICP = (port->iobase & 0xfe0) | 0x1f;
1327 save_ICP = inb_p(ICP);
1332 if (uart_console(port))
1335 /* forget possible initially masked and pending IRQ */
1336 probe_irq_off(probe_irq_on());
1337 save_mcr = serial8250_in_MCR(up);
1338 /* Synchronize UART_IER access against the console. */
1339 spin_lock_irq(&port->lock);
1340 save_ier = serial_in(up, UART_IER);
1341 spin_unlock_irq(&port->lock);
1342 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1344 irqs = probe_irq_on();
1345 serial8250_out_MCR(up, 0);
1347 if (port->flags & UPF_FOURPORT) {
1348 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1350 serial8250_out_MCR(up,
1351 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1353 /* Synchronize UART_IER access against the console. */
1354 spin_lock_irq(&port->lock);
1355 serial_out(up, UART_IER, UART_IER_ALL_INTR);
1356 spin_unlock_irq(&port->lock);
1357 serial_in(up, UART_LSR);
1358 serial_in(up, UART_RX);
1359 serial_in(up, UART_IIR);
1360 serial_in(up, UART_MSR);
1361 serial_out(up, UART_TX, 0xFF);
1363 irq = probe_irq_off(irqs);
1365 serial8250_out_MCR(up, save_mcr);
1366 /* Synchronize UART_IER access against the console. */
1367 spin_lock_irq(&port->lock);
1368 serial_out(up, UART_IER, save_ier);
1369 spin_unlock_irq(&port->lock);
1371 if (port->flags & UPF_FOURPORT)
1372 outb_p(save_ICP, ICP);
1374 if (uart_console(port))
1377 port->irq = (irq > 0) ? irq : 0;
1380 static void serial8250_stop_rx(struct uart_port *port)
1382 struct uart_8250_port *up = up_to_u8250p(port);
1384 /* Port locked to synchronize UART_IER access against the console. */
1385 lockdep_assert_held_once(&port->lock);
1387 serial8250_rpm_get(up);
1389 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1390 up->port.read_status_mask &= ~UART_LSR_DR;
1391 serial_port_out(port, UART_IER, up->ier);
1393 serial8250_rpm_put(up);
1397 * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback
1398 * @p: uart 8250 port
1400 * Generic callback usable by 8250 uart drivers to stop rs485 transmission.
1402 void serial8250_em485_stop_tx(struct uart_8250_port *p)
1404 unsigned char mcr = serial8250_in_MCR(p);
1406 /* Port locked to synchronize UART_IER access against the console. */
1407 lockdep_assert_held_once(&p->port.lock);
1409 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
1410 mcr |= UART_MCR_RTS;
1412 mcr &= ~UART_MCR_RTS;
1413 serial8250_out_MCR(p, mcr);
1416 * Empty the RX FIFO, we are not interested in anything
1417 * received during the half-duplex transmission.
1418 * Enable previously disabled RX interrupts.
1420 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1421 serial8250_clear_and_reinit_fifos(p);
1423 p->ier |= UART_IER_RLSI | UART_IER_RDI;
1424 serial_port_out(&p->port, UART_IER, p->ier);
1427 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx);
1429 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t)
1431 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1433 struct uart_8250_port *p = em485->port;
1434 unsigned long flags;
1436 serial8250_rpm_get(p);
1437 spin_lock_irqsave(&p->port.lock, flags);
1438 if (em485->active_timer == &em485->stop_tx_timer) {
1439 p->rs485_stop_tx(p);
1440 em485->active_timer = NULL;
1441 em485->tx_stopped = true;
1443 spin_unlock_irqrestore(&p->port.lock, flags);
1444 serial8250_rpm_put(p);
1446 return HRTIMER_NORESTART;
1449 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
1451 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
1454 static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay)
1456 struct uart_8250_em485 *em485 = p->em485;
1458 /* Port locked to synchronize UART_IER access against the console. */
1459 lockdep_assert_held_once(&p->port.lock);
1461 stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC;
1464 * rs485_stop_tx() is going to set RTS according to config
1465 * AND flush RX FIFO if required.
1467 if (stop_delay > 0) {
1468 em485->active_timer = &em485->stop_tx_timer;
1469 hrtimer_start(&em485->stop_tx_timer, ns_to_ktime(stop_delay), HRTIMER_MODE_REL);
1471 p->rs485_stop_tx(p);
1472 em485->active_timer = NULL;
1473 em485->tx_stopped = true;
1477 static inline void __stop_tx(struct uart_8250_port *p)
1479 struct uart_8250_em485 *em485 = p->em485;
1482 u16 lsr = serial_lsr_in(p);
1485 if (!(lsr & UART_LSR_THRE))
1488 * To provide required timing and allow FIFO transfer,
1489 * __stop_tx_rs485() must be called only when both FIFO and
1490 * shift register are empty. The device driver should either
1491 * enable interrupt on TEMT or set UART_CAP_NOTEMT that will
1492 * enlarge stop_tx_timer by the tx time of one frame to cover
1493 * for emptying of the shift register.
1495 if (!(lsr & UART_LSR_TEMT)) {
1496 if (!(p->capabilities & UART_CAP_NOTEMT))
1499 * RTS might get deasserted too early with the normal
1500 * frame timing formula. It seems to suggest THRE might
1501 * get asserted already during tx of the stop bit
1502 * rather than after it is fully sent.
1503 * Roughly estimate 1 extra bit here with / 7.
1505 stop_delay = p->port.frame_time + DIV_ROUND_UP(p->port.frame_time, 7);
1508 __stop_tx_rs485(p, stop_delay);
1511 if (serial8250_clear_THRI(p))
1512 serial8250_rpm_put_tx(p);
1515 static void serial8250_stop_tx(struct uart_port *port)
1517 struct uart_8250_port *up = up_to_u8250p(port);
1519 serial8250_rpm_get(up);
1523 * We really want to stop the transmitter from sending.
1525 if (port->type == PORT_16C950) {
1526 up->acr |= UART_ACR_TXDIS;
1527 serial_icr_write(up, UART_ACR, up->acr);
1529 serial8250_rpm_put(up);
1532 static inline void __start_tx(struct uart_port *port)
1534 struct uart_8250_port *up = up_to_u8250p(port);
1536 if (up->dma && !up->dma->tx_dma(up))
1539 if (serial8250_set_THRI(up)) {
1540 if (up->bugs & UART_BUG_TXEN) {
1541 u16 lsr = serial_lsr_in(up);
1543 if (lsr & UART_LSR_THRE)
1544 serial8250_tx_chars(up);
1549 * Re-enable the transmitter if we disabled it.
1551 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1552 up->acr &= ~UART_ACR_TXDIS;
1553 serial_icr_write(up, UART_ACR, up->acr);
1558 * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback
1559 * @up: uart 8250 port
1561 * Generic callback usable by 8250 uart drivers to start rs485 transmission.
1562 * Assumes that setting the RTS bit in the MCR register means RTS is high.
1563 * (Some chips use inverse semantics.) Further assumes that reception is
1564 * stoppable by disabling the UART_IER_RDI interrupt. (Some chips set the
1565 * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.)
1567 void serial8250_em485_start_tx(struct uart_8250_port *up)
1569 unsigned char mcr = serial8250_in_MCR(up);
1571 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1572 serial8250_stop_rx(&up->port);
1574 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1575 mcr |= UART_MCR_RTS;
1577 mcr &= ~UART_MCR_RTS;
1578 serial8250_out_MCR(up, mcr);
1580 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx);
1582 /* Returns false, if start_tx_timer was setup to defer TX start */
1583 static bool start_tx_rs485(struct uart_port *port)
1585 struct uart_8250_port *up = up_to_u8250p(port);
1586 struct uart_8250_em485 *em485 = up->em485;
1589 * While serial8250_em485_handle_stop_tx() is a noop if
1590 * em485->active_timer != &em485->stop_tx_timer, it might happen that
1591 * the timer is still armed and triggers only after the current bunch of
1592 * chars is send and em485->active_timer == &em485->stop_tx_timer again.
1593 * So cancel the timer. There is still a theoretical race condition if
1594 * the timer is already running and only comes around to check for
1595 * em485->active_timer when &em485->stop_tx_timer is armed again.
1597 if (em485->active_timer == &em485->stop_tx_timer)
1598 hrtimer_try_to_cancel(&em485->stop_tx_timer);
1600 em485->active_timer = NULL;
1602 if (em485->tx_stopped) {
1603 em485->tx_stopped = false;
1605 up->rs485_start_tx(up);
1607 if (up->port.rs485.delay_rts_before_send > 0) {
1608 em485->active_timer = &em485->start_tx_timer;
1609 start_hrtimer_ms(&em485->start_tx_timer,
1610 up->port.rs485.delay_rts_before_send);
1618 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t)
1620 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1622 struct uart_8250_port *p = em485->port;
1623 unsigned long flags;
1625 spin_lock_irqsave(&p->port.lock, flags);
1626 if (em485->active_timer == &em485->start_tx_timer) {
1627 __start_tx(&p->port);
1628 em485->active_timer = NULL;
1630 spin_unlock_irqrestore(&p->port.lock, flags);
1632 return HRTIMER_NORESTART;
1635 static void serial8250_start_tx(struct uart_port *port)
1637 struct uart_8250_port *up = up_to_u8250p(port);
1638 struct uart_8250_em485 *em485 = up->em485;
1640 /* Port locked to synchronize UART_IER access against the console. */
1641 lockdep_assert_held_once(&port->lock);
1643 if (!port->x_char && uart_circ_empty(&port->state->xmit))
1646 serial8250_rpm_get_tx(up);
1649 if ((em485->active_timer == &em485->start_tx_timer) ||
1650 !start_tx_rs485(port))
1656 static void serial8250_throttle(struct uart_port *port)
1658 port->throttle(port);
1661 static void serial8250_unthrottle(struct uart_port *port)
1663 port->unthrottle(port);
1666 static void serial8250_disable_ms(struct uart_port *port)
1668 struct uart_8250_port *up = up_to_u8250p(port);
1670 /* Port locked to synchronize UART_IER access against the console. */
1671 lockdep_assert_held_once(&port->lock);
1673 /* no MSR capabilities */
1674 if (up->bugs & UART_BUG_NOMSR)
1677 mctrl_gpio_disable_ms(up->gpios);
1679 up->ier &= ~UART_IER_MSI;
1680 serial_port_out(port, UART_IER, up->ier);
1683 static void serial8250_enable_ms(struct uart_port *port)
1685 struct uart_8250_port *up = up_to_u8250p(port);
1687 /* Port locked to synchronize UART_IER access against the console. */
1688 lockdep_assert_held_once(&port->lock);
1690 /* no MSR capabilities */
1691 if (up->bugs & UART_BUG_NOMSR)
1694 mctrl_gpio_enable_ms(up->gpios);
1696 up->ier |= UART_IER_MSI;
1698 serial8250_rpm_get(up);
1699 serial_port_out(port, UART_IER, up->ier);
1700 serial8250_rpm_put(up);
1703 void serial8250_read_char(struct uart_8250_port *up, u16 lsr)
1705 struct uart_port *port = &up->port;
1706 u8 ch, flag = TTY_NORMAL;
1708 if (likely(lsr & UART_LSR_DR))
1709 ch = serial_in(up, UART_RX);
1712 * Intel 82571 has a Serial Over Lan device that will
1713 * set UART_LSR_BI without setting UART_LSR_DR when
1714 * it receives a break. To avoid reading from the
1715 * receive buffer without UART_LSR_DR bit set, we
1716 * just force the read character to be 0
1722 lsr |= up->lsr_saved_flags;
1723 up->lsr_saved_flags = 0;
1725 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1726 if (lsr & UART_LSR_BI) {
1727 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1730 * We do the SysRQ and SAK checking
1731 * here because otherwise the break
1732 * may get masked by ignore_status_mask
1733 * or read_status_mask.
1735 if (uart_handle_break(port))
1737 } else if (lsr & UART_LSR_PE)
1738 port->icount.parity++;
1739 else if (lsr & UART_LSR_FE)
1740 port->icount.frame++;
1741 if (lsr & UART_LSR_OE)
1742 port->icount.overrun++;
1745 * Mask off conditions which should be ignored.
1747 lsr &= port->read_status_mask;
1749 if (lsr & UART_LSR_BI) {
1750 dev_dbg(port->dev, "handling break\n");
1752 } else if (lsr & UART_LSR_PE)
1754 else if (lsr & UART_LSR_FE)
1757 if (uart_prepare_sysrq_char(port, ch))
1760 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1762 EXPORT_SYMBOL_GPL(serial8250_read_char);
1765 * serial8250_rx_chars - Read characters. The first LSR value must be passed in.
1767 * Returns LSR bits. The caller should rely only on non-Rx related LSR bits
1768 * (such as THRE) because the LSR value might come from an already consumed
1771 u16 serial8250_rx_chars(struct uart_8250_port *up, u16 lsr)
1773 struct uart_port *port = &up->port;
1774 int max_count = 256;
1777 serial8250_read_char(up, lsr);
1778 if (--max_count == 0)
1780 lsr = serial_in(up, UART_LSR);
1781 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
1783 tty_flip_buffer_push(&port->state->port);
1786 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1788 void serial8250_tx_chars(struct uart_8250_port *up)
1790 struct uart_port *port = &up->port;
1791 struct circ_buf *xmit = &port->state->xmit;
1795 uart_xchar_out(port, UART_TX);
1798 if (uart_tx_stopped(port)) {
1799 serial8250_stop_tx(port);
1802 if (uart_circ_empty(xmit)) {
1807 count = up->tx_loadsz;
1809 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1810 if (up->bugs & UART_BUG_TXRACE) {
1812 * The Aspeed BMC virtual UARTs have a bug where data
1813 * may get stuck in the BMC's Tx FIFO from bursts of
1814 * writes on the APB interface.
1816 * Delay back-to-back writes by a read cycle to avoid
1817 * stalling the VUART. Read a register that won't have
1818 * side-effects and discard the result.
1820 serial_in(up, UART_SCR);
1822 uart_xmit_advance(port, 1);
1823 if (uart_circ_empty(xmit))
1825 if ((up->capabilities & UART_CAP_HFIFO) &&
1826 !uart_lsr_tx_empty(serial_in(up, UART_LSR)))
1828 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */
1829 if ((up->capabilities & UART_CAP_MINI) &&
1830 !(serial_in(up, UART_LSR) & UART_LSR_THRE))
1832 } while (--count > 0);
1834 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1835 uart_write_wakeup(port);
1838 * With RPM enabled, we have to wait until the FIFO is empty before the
1839 * HW can go idle. So we get here once again with empty FIFO and disable
1840 * the interrupt and RPM in __stop_tx()
1842 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1845 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1847 /* Caller holds uart port lock */
1848 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1850 struct uart_port *port = &up->port;
1851 unsigned int status = serial_in(up, UART_MSR);
1853 status |= up->msr_saved_flags;
1854 up->msr_saved_flags = 0;
1855 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1856 port->state != NULL) {
1857 if (status & UART_MSR_TERI)
1859 if (status & UART_MSR_DDSR)
1861 if (status & UART_MSR_DDCD)
1862 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1863 if (status & UART_MSR_DCTS)
1864 uart_handle_cts_change(port, status & UART_MSR_CTS);
1866 wake_up_interruptible(&port->state->port.delta_msr_wait);
1871 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1873 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1875 switch (iir & 0x3f) {
1878 * Postpone DMA or not decision to IIR_RDI or IIR_RX_TIMEOUT
1879 * because it's impossible to do an informed decision about
1880 * that with IIR_THRI.
1882 * This also fixes one known DMA Rx corruption issue where
1883 * DR is asserted but DMA Rx only gets a corrupted zero byte
1888 if (!up->dma->rx_running)
1892 case UART_IIR_RX_TIMEOUT:
1893 serial8250_rx_dma_flush(up);
1896 return up->dma->rx_dma(up);
1900 * This handles the interrupt from one port.
1902 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1904 struct uart_8250_port *up = up_to_u8250p(port);
1905 struct tty_port *tport = &port->state->port;
1906 bool skip_rx = false;
1907 unsigned long flags;
1910 if (iir & UART_IIR_NO_INT)
1913 spin_lock_irqsave(&port->lock, flags);
1915 status = serial_lsr_in(up);
1918 * If port is stopped and there are no error conditions in the
1919 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer
1920 * overflow. Not servicing, RX FIFO would trigger auto HW flow
1921 * control when FIFO occupancy reaches preset threshold, thus
1922 * halting RX. This only works when auto HW flow control is
1925 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) &&
1926 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) &&
1927 !(port->read_status_mask & UART_LSR_DR))
1930 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) {
1931 if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
1932 pm_wakeup_event(tport->tty->dev, 0);
1933 if (!up->dma || handle_rx_dma(up, iir))
1934 status = serial8250_rx_chars(up, status);
1936 serial8250_modem_status(up);
1937 if ((status & UART_LSR_THRE) && (up->ier & UART_IER_THRI)) {
1938 if (!up->dma || up->dma->tx_err)
1939 serial8250_tx_chars(up);
1940 else if (!up->dma->tx_running)
1944 uart_unlock_and_check_sysrq_irqrestore(port, flags);
1948 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1950 static int serial8250_default_handle_irq(struct uart_port *port)
1952 struct uart_8250_port *up = up_to_u8250p(port);
1956 serial8250_rpm_get(up);
1958 iir = serial_port_in(port, UART_IIR);
1959 ret = serial8250_handle_irq(port, iir);
1961 serial8250_rpm_put(up);
1966 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1967 * have a programmable TX threshold that triggers the THRE interrupt in
1968 * the IIR register. In this case, the THRE interrupt indicates the FIFO
1969 * has space available. Load it up with tx_loadsz bytes.
1971 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1973 unsigned long flags;
1974 unsigned int iir = serial_port_in(port, UART_IIR);
1976 /* TX Threshold IRQ triggered so load up FIFO */
1977 if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1978 struct uart_8250_port *up = up_to_u8250p(port);
1980 spin_lock_irqsave(&port->lock, flags);
1981 serial8250_tx_chars(up);
1982 spin_unlock_irqrestore(&port->lock, flags);
1985 iir = serial_port_in(port, UART_IIR);
1986 return serial8250_handle_irq(port, iir);
1989 static unsigned int serial8250_tx_empty(struct uart_port *port)
1991 struct uart_8250_port *up = up_to_u8250p(port);
1992 unsigned int result = 0;
1993 unsigned long flags;
1995 serial8250_rpm_get(up);
1997 spin_lock_irqsave(&port->lock, flags);
1998 if (!serial8250_tx_dma_running(up) && uart_lsr_tx_empty(serial_lsr_in(up)))
1999 result = TIOCSER_TEMT;
2000 spin_unlock_irqrestore(&port->lock, flags);
2002 serial8250_rpm_put(up);
2007 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
2009 struct uart_8250_port *up = up_to_u8250p(port);
2010 unsigned int status;
2013 serial8250_rpm_get(up);
2014 status = serial8250_modem_status(up);
2015 serial8250_rpm_put(up);
2017 val = serial8250_MSR_to_TIOCM(status);
2019 return mctrl_gpio_get(up->gpios, &val);
2023 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
2025 static unsigned int serial8250_get_mctrl(struct uart_port *port)
2027 if (port->get_mctrl)
2028 return port->get_mctrl(port);
2029 return serial8250_do_get_mctrl(port);
2032 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
2034 struct uart_8250_port *up = up_to_u8250p(port);
2037 mcr = serial8250_TIOCM_to_MCR(mctrl);
2041 serial8250_out_MCR(up, mcr);
2043 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
2045 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
2047 if (port->rs485.flags & SER_RS485_ENABLED)
2050 if (port->set_mctrl)
2051 port->set_mctrl(port, mctrl);
2053 serial8250_do_set_mctrl(port, mctrl);
2056 static void serial8250_break_ctl(struct uart_port *port, int break_state)
2058 struct uart_8250_port *up = up_to_u8250p(port);
2059 unsigned long flags;
2061 serial8250_rpm_get(up);
2062 spin_lock_irqsave(&port->lock, flags);
2063 if (break_state == -1)
2064 up->lcr |= UART_LCR_SBC;
2066 up->lcr &= ~UART_LCR_SBC;
2067 serial_port_out(port, UART_LCR, up->lcr);
2068 spin_unlock_irqrestore(&port->lock, flags);
2069 serial8250_rpm_put(up);
2072 static void wait_for_lsr(struct uart_8250_port *up, int bits)
2074 unsigned int status, tmout = 10000;
2076 /* Wait up to 10ms for the character(s) to be sent. */
2078 status = serial_lsr_in(up);
2080 if ((status & bits) == bits)
2085 touch_nmi_watchdog();
2090 * Wait for transmitter & holding register to empty
2092 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2096 wait_for_lsr(up, bits);
2098 /* Wait up to 1s for flow control if necessary */
2099 if (up->port.flags & UPF_CONS_FLOW) {
2100 for (tmout = 1000000; tmout; tmout--) {
2101 unsigned int msr = serial_in(up, UART_MSR);
2102 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2103 if (msr & UART_MSR_CTS)
2106 touch_nmi_watchdog();
2111 #ifdef CONFIG_CONSOLE_POLL
2113 * Console polling routines for writing and reading from the uart while
2114 * in an interrupt or debug context.
2117 static int serial8250_get_poll_char(struct uart_port *port)
2119 struct uart_8250_port *up = up_to_u8250p(port);
2123 serial8250_rpm_get(up);
2125 lsr = serial_port_in(port, UART_LSR);
2127 if (!(lsr & UART_LSR_DR)) {
2128 status = NO_POLL_CHAR;
2132 status = serial_port_in(port, UART_RX);
2134 serial8250_rpm_put(up);
2139 static void serial8250_put_poll_char(struct uart_port *port,
2143 struct uart_8250_port *up = up_to_u8250p(port);
2146 * Normally the port is locked to synchronize UART_IER access
2147 * against the console. However, this function is only used by
2148 * KDB/KGDB, where it may not be possible to acquire the port
2149 * lock because all other CPUs are quiesced. The quiescence
2150 * should allow safe lockless usage here.
2153 serial8250_rpm_get(up);
2155 * First save the IER then disable the interrupts
2157 ier = serial_port_in(port, UART_IER);
2158 serial8250_clear_IER(up);
2160 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
2162 * Send the character out.
2164 serial_port_out(port, UART_TX, c);
2167 * Finally, wait for transmitter to become empty
2168 * and restore the IER
2170 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
2171 serial_port_out(port, UART_IER, ier);
2172 serial8250_rpm_put(up);
2175 #endif /* CONFIG_CONSOLE_POLL */
2177 int serial8250_do_startup(struct uart_port *port)
2179 struct uart_8250_port *up = up_to_u8250p(port);
2180 unsigned long flags;
2185 if (!port->fifosize)
2186 port->fifosize = uart_config[port->type].fifo_size;
2188 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2189 if (!up->capabilities)
2190 up->capabilities = uart_config[port->type].flags;
2193 if (port->iotype != up->cur_iotype)
2194 set_io_from_upio(port);
2196 serial8250_rpm_get(up);
2197 if (port->type == PORT_16C950) {
2199 * Wake up and initialize UART
2201 * Synchronize UART_IER access against the console.
2203 spin_lock_irqsave(&port->lock, flags);
2205 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2206 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2207 serial_port_out(port, UART_IER, 0);
2208 serial_port_out(port, UART_LCR, 0);
2209 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2210 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2211 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2212 serial_port_out(port, UART_LCR, 0);
2213 spin_unlock_irqrestore(&port->lock, flags);
2216 if (port->type == PORT_DA830) {
2220 * Synchronize UART_IER access against the console.
2222 spin_lock_irqsave(&port->lock, flags);
2223 serial_port_out(port, UART_IER, 0);
2224 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
2225 spin_unlock_irqrestore(&port->lock, flags);
2228 /* Enable Tx, Rx and free run mode */
2229 serial_port_out(port, UART_DA830_PWREMU_MGMT,
2230 UART_DA830_PWREMU_MGMT_UTRST |
2231 UART_DA830_PWREMU_MGMT_URRST |
2232 UART_DA830_PWREMU_MGMT_FREE);
2235 if (port->type == PORT_NPCM) {
2237 * Nuvoton calls the scratch register 'UART_TOR' (timeout
2238 * register). Enable it, and set TIOC (timeout interrupt
2239 * comparator) to be 0x20 for correct operation.
2241 serial_port_out(port, UART_NPCM_TOR, UART_NPCM_TOIE | 0x20);
2244 #ifdef CONFIG_SERIAL_8250_RSA
2246 * If this is an RSA port, see if we can kick it up to the
2247 * higher speed clock.
2253 * Clear the FIFO buffers and disable them.
2254 * (they will be reenabled in set_termios())
2256 serial8250_clear_fifos(up);
2259 * Clear the interrupt registers.
2261 serial_port_in(port, UART_LSR);
2262 serial_port_in(port, UART_RX);
2263 serial_port_in(port, UART_IIR);
2264 serial_port_in(port, UART_MSR);
2267 * At this point, there's no way the LSR could still be 0xff;
2268 * if it is, then bail out, because there's likely no UART
2271 if (!(port->flags & UPF_BUGGY_UART) &&
2272 (serial_port_in(port, UART_LSR) == 0xff)) {
2273 dev_info_ratelimited(port->dev, "LSR safety check engaged!\n");
2279 * For a XR16C850, we need to set the trigger levels
2281 if (port->type == PORT_16850) {
2284 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2286 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2287 serial_port_out(port, UART_FCTR,
2288 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2289 serial_port_out(port, UART_TRG, UART_TRG_96);
2290 serial_port_out(port, UART_FCTR,
2291 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2292 serial_port_out(port, UART_TRG, UART_TRG_96);
2294 serial_port_out(port, UART_LCR, 0);
2298 * For the Altera 16550 variants, set TX threshold trigger level.
2300 if (((port->type == PORT_ALTR_16550_F32) ||
2301 (port->type == PORT_ALTR_16550_F64) ||
2302 (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2303 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2304 if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2305 dev_err(port->dev, "TX FIFO Threshold errors, skipping\n");
2307 serial_port_out(port, UART_ALTR_AFR,
2308 UART_ALTR_EN_TXFIFO_LW);
2309 serial_port_out(port, UART_ALTR_TX_LOW,
2310 port->fifosize - up->tx_loadsz);
2311 port->handle_irq = serial8250_tx_threshold_handle_irq;
2315 /* Check if we need to have shared IRQs */
2316 if (port->irq && (up->port.flags & UPF_SHARE_IRQ))
2317 up->port.irqflags |= IRQF_SHARED;
2319 retval = up->ops->setup_irq(up);
2323 if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) {
2326 if (port->irqflags & IRQF_SHARED)
2327 disable_irq_nosync(port->irq);
2330 * Test for UARTs that do not reassert THRE when the
2331 * transmitter is idle and the interrupt has already
2332 * been cleared. Real 16550s should always reassert
2333 * this interrupt whenever the transmitter is idle and
2334 * the interrupt is enabled. Delays are necessary to
2335 * allow register changes to become visible.
2337 * Synchronize UART_IER access against the console.
2339 spin_lock_irqsave(&port->lock, flags);
2341 wait_for_xmitr(up, UART_LSR_THRE);
2342 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2343 udelay(1); /* allow THRE to set */
2344 iir1 = serial_port_in(port, UART_IIR);
2345 serial_port_out(port, UART_IER, 0);
2346 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2347 udelay(1); /* allow a working UART time to re-assert THRE */
2348 iir = serial_port_in(port, UART_IIR);
2349 serial_port_out(port, UART_IER, 0);
2351 spin_unlock_irqrestore(&port->lock, flags);
2353 if (port->irqflags & IRQF_SHARED)
2354 enable_irq(port->irq);
2357 * If the interrupt is not reasserted, or we otherwise
2358 * don't trust the iir, setup a timer to kick the UART
2359 * on a regular basis.
2361 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2362 up->port.flags & UPF_BUG_THRE) {
2363 up->bugs |= UART_BUG_THRE;
2367 up->ops->setup_timer(up);
2370 * Now, initialize the UART
2372 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2374 spin_lock_irqsave(&port->lock, flags);
2375 if (up->port.flags & UPF_FOURPORT) {
2377 up->port.mctrl |= TIOCM_OUT1;
2380 * Most PC uarts need OUT2 raised to enable interrupts.
2383 up->port.mctrl |= TIOCM_OUT2;
2385 serial8250_set_mctrl(port, port->mctrl);
2388 * Serial over Lan (SoL) hack:
2389 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be
2390 * used for Serial Over Lan. Those chips take a longer time than a
2391 * normal serial device to signalize that a transmission data was
2392 * queued. Due to that, the above test generally fails. One solution
2393 * would be to delay the reading of iir. However, this is not
2394 * reliable, since the timeout is variable. So, let's just don't
2395 * test if we receive TX irq. This way, we'll never enable
2398 if (up->port.quirks & UPQ_NO_TXEN_TEST)
2399 goto dont_test_tx_en;
2402 * Do a quick test to see if we receive an interrupt when we enable
2405 serial_port_out(port, UART_IER, UART_IER_THRI);
2406 lsr = serial_port_in(port, UART_LSR);
2407 iir = serial_port_in(port, UART_IIR);
2408 serial_port_out(port, UART_IER, 0);
2410 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2411 if (!(up->bugs & UART_BUG_TXEN)) {
2412 up->bugs |= UART_BUG_TXEN;
2413 dev_dbg(port->dev, "enabling bad tx status workarounds\n");
2416 up->bugs &= ~UART_BUG_TXEN;
2420 spin_unlock_irqrestore(&port->lock, flags);
2423 * Clear the interrupt registers again for luck, and clear the
2424 * saved flags to avoid getting false values from polling
2425 * routines or the previous session.
2427 serial_port_in(port, UART_LSR);
2428 serial_port_in(port, UART_RX);
2429 serial_port_in(port, UART_IIR);
2430 serial_port_in(port, UART_MSR);
2431 up->lsr_saved_flags = 0;
2432 up->msr_saved_flags = 0;
2435 * Request DMA channels for both RX and TX.
2438 const char *msg = NULL;
2440 if (uart_console(port))
2441 msg = "forbid DMA for kernel console";
2442 else if (serial8250_request_dma(up))
2443 msg = "failed to request DMA";
2445 dev_warn_ratelimited(port->dev, "%s\n", msg);
2451 * Set the IER shadow for rx interrupts but defer actual interrupt
2452 * enable until after the FIFOs are enabled; otherwise, an already-
2453 * active sender can swamp the interrupt handler with "too much work".
2455 up->ier = UART_IER_RLSI | UART_IER_RDI;
2457 if (port->flags & UPF_FOURPORT) {
2460 * Enable interrupts on the AST Fourport board
2462 icp = (port->iobase & 0xfe0) | 0x01f;
2468 serial8250_rpm_put(up);
2471 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2473 static int serial8250_startup(struct uart_port *port)
2476 return port->startup(port);
2477 return serial8250_do_startup(port);
2480 void serial8250_do_shutdown(struct uart_port *port)
2482 struct uart_8250_port *up = up_to_u8250p(port);
2483 unsigned long flags;
2485 serial8250_rpm_get(up);
2487 * Disable interrupts from this port
2489 * Synchronize UART_IER access against the console.
2491 spin_lock_irqsave(&port->lock, flags);
2493 serial_port_out(port, UART_IER, 0);
2494 spin_unlock_irqrestore(&port->lock, flags);
2496 synchronize_irq(port->irq);
2499 serial8250_release_dma(up);
2501 spin_lock_irqsave(&port->lock, flags);
2502 if (port->flags & UPF_FOURPORT) {
2503 /* reset interrupts on the AST Fourport board */
2504 inb((port->iobase & 0xfe0) | 0x1f);
2505 port->mctrl |= TIOCM_OUT1;
2507 port->mctrl &= ~TIOCM_OUT2;
2509 serial8250_set_mctrl(port, port->mctrl);
2510 spin_unlock_irqrestore(&port->lock, flags);
2513 * Disable break condition and FIFOs
2515 serial_port_out(port, UART_LCR,
2516 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2517 serial8250_clear_fifos(up);
2519 #ifdef CONFIG_SERIAL_8250_RSA
2521 * Reset the RSA board back to 115kbps compat mode.
2527 * Read data port to reset things, and then unlink from
2530 serial_port_in(port, UART_RX);
2531 serial8250_rpm_put(up);
2533 up->ops->release_irq(up);
2535 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2537 static void serial8250_shutdown(struct uart_port *port)
2540 port->shutdown(port);
2542 serial8250_do_shutdown(port);
2545 /* Nuvoton NPCM UARTs have a custom divisor calculation */
2546 static unsigned int npcm_get_divisor(struct uart_8250_port *up,
2549 struct uart_port *port = &up->port;
2551 return DIV_ROUND_CLOSEST(port->uartclk, 16 * baud + 2) - 2;
2554 static unsigned int serial8250_do_get_divisor(struct uart_port *port,
2558 upf_t magic_multiplier = port->flags & UPF_MAGIC_MULTIPLIER;
2559 struct uart_8250_port *up = up_to_u8250p(port);
2563 * Handle magic divisors for baud rates above baud_base on SMSC
2564 * Super I/O chips. We clamp custom rates from clk/6 and clk/12
2565 * up to clk/4 (0x8001) and clk/8 (0x8002) respectively. These
2566 * magic divisors actually reprogram the baud rate generator's
2567 * reference clock derived from chips's 14.318MHz clock input.
2569 * Documentation claims that with these magic divisors the base
2570 * frequencies of 7.3728MHz and 3.6864MHz are used respectively
2571 * for the extra baud rates of 460800bps and 230400bps rather
2572 * than the usual base frequency of 1.8462MHz. However empirical
2573 * evidence contradicts that.
2575 * Instead bit 7 of the DLM register (bit 15 of the divisor) is
2576 * effectively used as a clock prescaler selection bit for the
2577 * base frequency of 7.3728MHz, always used. If set to 0, then
2578 * the base frequency is divided by 4 for use by the Baud Rate
2579 * Generator, for the usual arrangement where the value of 1 of
2580 * the divisor produces the baud rate of 115200bps. Conversely,
2581 * if set to 1 and high-speed operation has been enabled with the
2582 * Serial Port Mode Register in the Device Configuration Space,
2583 * then the base frequency is supplied directly to the Baud Rate
2584 * Generator, so for the divisor values of 0x8001, 0x8002, 0x8003,
2585 * 0x8004, etc. the respective baud rates produced are 460800bps,
2586 * 230400bps, 153600bps, 115200bps, etc.
2588 * In all cases only low 15 bits of the divisor are used to divide
2589 * the baud base and therefore 32767 is the maximum divisor value
2590 * possible, even though documentation says that the programmable
2591 * Baud Rate Generator is capable of dividing the internal PLL
2592 * clock by any divisor from 1 to 65535.
2594 if (magic_multiplier && baud >= port->uartclk / 6)
2596 else if (magic_multiplier && baud >= port->uartclk / 12)
2598 else if (up->port.type == PORT_NPCM)
2599 quot = npcm_get_divisor(up, baud);
2601 quot = uart_get_divisor(port, baud);
2604 * Oxford Semi 952 rev B workaround
2606 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2612 static unsigned int serial8250_get_divisor(struct uart_port *port,
2616 if (port->get_divisor)
2617 return port->get_divisor(port, baud, frac);
2619 return serial8250_do_get_divisor(port, baud, frac);
2622 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2627 cval = UART_LCR_WLEN(tty_get_char_size(c_cflag));
2629 if (c_cflag & CSTOPB)
2630 cval |= UART_LCR_STOP;
2631 if (c_cflag & PARENB)
2632 cval |= UART_LCR_PARITY;
2633 if (!(c_cflag & PARODD))
2634 cval |= UART_LCR_EPAR;
2635 if (c_cflag & CMSPAR)
2636 cval |= UART_LCR_SPAR;
2641 void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud,
2642 unsigned int quot, unsigned int quot_frac)
2644 struct uart_8250_port *up = up_to_u8250p(port);
2646 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2647 if (is_omap1510_8250(up)) {
2648 if (baud == 115200) {
2650 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2652 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2656 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2657 * otherwise just set DLAB
2659 if (up->capabilities & UART_NATSEMI)
2660 serial_port_out(port, UART_LCR, 0xe0);
2662 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2664 serial_dl_write(up, quot);
2666 EXPORT_SYMBOL_GPL(serial8250_do_set_divisor);
2668 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2669 unsigned int quot, unsigned int quot_frac)
2671 if (port->set_divisor)
2672 port->set_divisor(port, baud, quot, quot_frac);
2674 serial8250_do_set_divisor(port, baud, quot, quot_frac);
2677 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2678 struct ktermios *termios,
2679 const struct ktermios *old)
2681 unsigned int tolerance = port->uartclk / 100;
2686 * Handle magic divisors for baud rates above baud_base on SMSC
2687 * Super I/O chips. Enable custom rates of clk/4 and clk/8, but
2688 * disable divisor values beyond 32767, which are unavailable.
2690 if (port->flags & UPF_MAGIC_MULTIPLIER) {
2691 min = port->uartclk / 16 / UART_DIV_MAX >> 1;
2692 max = (port->uartclk + tolerance) / 4;
2694 min = port->uartclk / 16 / UART_DIV_MAX;
2695 max = (port->uartclk + tolerance) / 16;
2699 * Ask the core to calculate the divisor for us.
2700 * Allow 1% tolerance at the upper limit so uart clks marginally
2701 * slower than nominal still match standard baud rates without
2702 * causing transmission errors.
2704 return uart_get_baud_rate(port, termios, old, min, max);
2708 * Note in order to avoid the tty port mutex deadlock don't use the next method
2709 * within the uart port callbacks. Primarily it's supposed to be utilized to
2710 * handle a sudden reference clock rate change.
2712 void serial8250_update_uartclk(struct uart_port *port, unsigned int uartclk)
2714 struct uart_8250_port *up = up_to_u8250p(port);
2715 struct tty_port *tport = &port->state->port;
2716 unsigned int baud, quot, frac = 0;
2717 struct ktermios *termios;
2718 struct tty_struct *tty;
2719 unsigned long flags;
2721 tty = tty_port_tty_get(tport);
2723 mutex_lock(&tport->mutex);
2724 port->uartclk = uartclk;
2725 mutex_unlock(&tport->mutex);
2729 down_write(&tty->termios_rwsem);
2730 mutex_lock(&tport->mutex);
2732 if (port->uartclk == uartclk)
2735 port->uartclk = uartclk;
2737 if (!tty_port_initialized(tport))
2740 termios = &tty->termios;
2742 baud = serial8250_get_baud_rate(port, termios, NULL);
2743 quot = serial8250_get_divisor(port, baud, &frac);
2745 serial8250_rpm_get(up);
2746 spin_lock_irqsave(&port->lock, flags);
2748 uart_update_timeout(port, termios->c_cflag, baud);
2750 serial8250_set_divisor(port, baud, quot, frac);
2751 serial_port_out(port, UART_LCR, up->lcr);
2753 spin_unlock_irqrestore(&port->lock, flags);
2754 serial8250_rpm_put(up);
2757 mutex_unlock(&tport->mutex);
2758 up_write(&tty->termios_rwsem);
2761 EXPORT_SYMBOL_GPL(serial8250_update_uartclk);
2764 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2765 const struct ktermios *old)
2767 struct uart_8250_port *up = up_to_u8250p(port);
2769 unsigned long flags;
2770 unsigned int baud, quot, frac = 0;
2772 if (up->capabilities & UART_CAP_MINI) {
2773 termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR);
2774 if ((termios->c_cflag & CSIZE) == CS5 ||
2775 (termios->c_cflag & CSIZE) == CS6)
2776 termios->c_cflag = (termios->c_cflag & ~CSIZE) | CS7;
2778 cval = serial8250_compute_lcr(up, termios->c_cflag);
2780 baud = serial8250_get_baud_rate(port, termios, old);
2781 quot = serial8250_get_divisor(port, baud, &frac);
2784 * Ok, we're now changing the port state. Do it with
2785 * interrupts disabled.
2787 * Synchronize UART_IER access against the console.
2789 serial8250_rpm_get(up);
2790 spin_lock_irqsave(&port->lock, flags);
2792 up->lcr = cval; /* Save computed LCR */
2794 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2795 if (baud < 2400 && !up->dma) {
2796 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2797 up->fcr |= UART_FCR_TRIGGER_1;
2802 * MCR-based auto flow control. When AFE is enabled, RTS will be
2803 * deasserted when the receive FIFO contains more characters than
2804 * the trigger, or the MCR RTS bit is cleared.
2806 if (up->capabilities & UART_CAP_AFE) {
2807 up->mcr &= ~UART_MCR_AFE;
2808 if (termios->c_cflag & CRTSCTS)
2809 up->mcr |= UART_MCR_AFE;
2813 * Update the per-port timeout.
2815 uart_update_timeout(port, termios->c_cflag, baud);
2817 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2818 if (termios->c_iflag & INPCK)
2819 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2820 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2821 port->read_status_mask |= UART_LSR_BI;
2824 * Characters to ignore
2826 port->ignore_status_mask = 0;
2827 if (termios->c_iflag & IGNPAR)
2828 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2829 if (termios->c_iflag & IGNBRK) {
2830 port->ignore_status_mask |= UART_LSR_BI;
2832 * If we're ignoring parity and break indicators,
2833 * ignore overruns too (for real raw support).
2835 if (termios->c_iflag & IGNPAR)
2836 port->ignore_status_mask |= UART_LSR_OE;
2840 * ignore all characters if CREAD is not set
2842 if ((termios->c_cflag & CREAD) == 0)
2843 port->ignore_status_mask |= UART_LSR_DR;
2846 * CTS flow control flag and modem status interrupts
2848 up->ier &= ~UART_IER_MSI;
2849 if (!(up->bugs & UART_BUG_NOMSR) &&
2850 UART_ENABLE_MS(&up->port, termios->c_cflag))
2851 up->ier |= UART_IER_MSI;
2852 if (up->capabilities & UART_CAP_UUE)
2853 up->ier |= UART_IER_UUE;
2854 if (up->capabilities & UART_CAP_RTOIE)
2855 up->ier |= UART_IER_RTOIE;
2857 serial_port_out(port, UART_IER, up->ier);
2859 if (up->capabilities & UART_CAP_EFR) {
2860 unsigned char efr = 0;
2862 * TI16C752/Startech hardware flow control. FIXME:
2863 * - TI16C752 requires control thresholds to be set.
2864 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2866 if (termios->c_cflag & CRTSCTS)
2867 efr |= UART_EFR_CTS;
2869 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2870 if (port->flags & UPF_EXAR_EFR)
2871 serial_port_out(port, UART_XR_EFR, efr);
2873 serial_port_out(port, UART_EFR, efr);
2876 serial8250_set_divisor(port, baud, quot, frac);
2879 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2880 * is written without DLAB set, this mode will be disabled.
2882 if (port->type == PORT_16750)
2883 serial_port_out(port, UART_FCR, up->fcr);
2885 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
2886 if (port->type != PORT_16750) {
2887 /* emulated UARTs (Lucent Venus 167x) need two steps */
2888 if (up->fcr & UART_FCR_ENABLE_FIFO)
2889 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2890 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
2892 serial8250_set_mctrl(port, port->mctrl);
2893 spin_unlock_irqrestore(&port->lock, flags);
2894 serial8250_rpm_put(up);
2896 /* Don't rewrite B0 */
2897 if (tty_termios_baud_rate(termios))
2898 tty_termios_encode_baud_rate(termios, baud, baud);
2900 EXPORT_SYMBOL(serial8250_do_set_termios);
2903 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2904 const struct ktermios *old)
2906 if (port->set_termios)
2907 port->set_termios(port, termios, old);
2909 serial8250_do_set_termios(port, termios, old);
2912 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios)
2914 if (termios->c_line == N_PPS) {
2915 port->flags |= UPF_HARDPPS_CD;
2916 spin_lock_irq(&port->lock);
2917 serial8250_enable_ms(port);
2918 spin_unlock_irq(&port->lock);
2920 port->flags &= ~UPF_HARDPPS_CD;
2921 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2922 spin_lock_irq(&port->lock);
2923 serial8250_disable_ms(port);
2924 spin_unlock_irq(&port->lock);
2928 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc);
2931 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2933 if (port->set_ldisc)
2934 port->set_ldisc(port, termios);
2936 serial8250_do_set_ldisc(port, termios);
2939 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2940 unsigned int oldstate)
2942 struct uart_8250_port *p = up_to_u8250p(port);
2944 serial8250_set_sleep(p, state != 0);
2946 EXPORT_SYMBOL(serial8250_do_pm);
2949 serial8250_pm(struct uart_port *port, unsigned int state,
2950 unsigned int oldstate)
2953 port->pm(port, state, oldstate);
2955 serial8250_do_pm(port, state, oldstate);
2958 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2960 if (pt->port.mapsize)
2961 return pt->port.mapsize;
2962 if (is_omap1_8250(pt))
2963 return 0x16 << pt->port.regshift;
2965 return 8 << pt->port.regshift;
2969 * Resource handling.
2971 static int serial8250_request_std_resource(struct uart_8250_port *up)
2973 unsigned int size = serial8250_port_size(up);
2974 struct uart_port *port = &up->port;
2977 switch (port->iotype) {
2984 if (!port->mapbase) {
2989 if (!request_mem_region(port->mapbase, size, "serial")) {
2994 if (port->flags & UPF_IOREMAP) {
2995 port->membase = ioremap(port->mapbase, size);
2996 if (!port->membase) {
2997 release_mem_region(port->mapbase, size);
3005 if (!request_region(port->iobase, size, "serial"))
3012 static void serial8250_release_std_resource(struct uart_8250_port *up)
3014 unsigned int size = serial8250_port_size(up);
3015 struct uart_port *port = &up->port;
3017 switch (port->iotype) {
3027 if (port->flags & UPF_IOREMAP) {
3028 iounmap(port->membase);
3029 port->membase = NULL;
3032 release_mem_region(port->mapbase, size);
3037 release_region(port->iobase, size);
3042 static void serial8250_release_port(struct uart_port *port)
3044 struct uart_8250_port *up = up_to_u8250p(port);
3046 serial8250_release_std_resource(up);
3049 static int serial8250_request_port(struct uart_port *port)
3051 struct uart_8250_port *up = up_to_u8250p(port);
3053 return serial8250_request_std_resource(up);
3056 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
3058 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3059 unsigned char bytes;
3061 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
3063 return bytes ? bytes : -EOPNOTSUPP;
3066 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
3068 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3071 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
3074 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
3075 if (bytes < conf_type->rxtrig_bytes[i])
3076 /* Use the nearest lower value */
3077 return (--i) << UART_FCR_R_TRIG_SHIFT;
3080 return UART_FCR_R_TRIG_11;
3083 static int do_get_rxtrig(struct tty_port *port)
3085 struct uart_state *state = container_of(port, struct uart_state, port);
3086 struct uart_port *uport = state->uart_port;
3087 struct uart_8250_port *up = up_to_u8250p(uport);
3089 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3092 return fcr_get_rxtrig_bytes(up);
3095 static int do_serial8250_get_rxtrig(struct tty_port *port)
3099 mutex_lock(&port->mutex);
3100 rxtrig_bytes = do_get_rxtrig(port);
3101 mutex_unlock(&port->mutex);
3103 return rxtrig_bytes;
3106 static ssize_t rx_trig_bytes_show(struct device *dev,
3107 struct device_attribute *attr, char *buf)
3109 struct tty_port *port = dev_get_drvdata(dev);
3112 rxtrig_bytes = do_serial8250_get_rxtrig(port);
3113 if (rxtrig_bytes < 0)
3114 return rxtrig_bytes;
3116 return sysfs_emit(buf, "%d\n", rxtrig_bytes);
3119 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
3121 struct uart_state *state = container_of(port, struct uart_state, port);
3122 struct uart_port *uport = state->uart_port;
3123 struct uart_8250_port *up = up_to_u8250p(uport);
3126 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3129 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
3133 serial8250_clear_fifos(up);
3134 up->fcr &= ~UART_FCR_TRIGGER_MASK;
3135 up->fcr |= (unsigned char)rxtrig;
3136 serial_out(up, UART_FCR, up->fcr);
3140 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
3144 mutex_lock(&port->mutex);
3145 ret = do_set_rxtrig(port, bytes);
3146 mutex_unlock(&port->mutex);
3151 static ssize_t rx_trig_bytes_store(struct device *dev,
3152 struct device_attribute *attr, const char *buf, size_t count)
3154 struct tty_port *port = dev_get_drvdata(dev);
3155 unsigned char bytes;
3161 ret = kstrtou8(buf, 10, &bytes);
3165 ret = do_serial8250_set_rxtrig(port, bytes);
3172 static DEVICE_ATTR_RW(rx_trig_bytes);
3174 static struct attribute *serial8250_dev_attrs[] = {
3175 &dev_attr_rx_trig_bytes.attr,
3179 static struct attribute_group serial8250_dev_attr_group = {
3180 .attrs = serial8250_dev_attrs,
3183 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3185 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3187 if (conf_type->rxtrig_bytes[0])
3188 up->port.attr_group = &serial8250_dev_attr_group;
3191 static void serial8250_config_port(struct uart_port *port, int flags)
3193 struct uart_8250_port *up = up_to_u8250p(port);
3197 * Find the region that we can probe for. This in turn
3198 * tells us whether we can probe for the type of port.
3200 ret = serial8250_request_std_resource(up);
3204 if (port->iotype != up->cur_iotype)
3205 set_io_from_upio(port);
3207 if (flags & UART_CONFIG_TYPE)
3210 /* HW bugs may trigger IRQ while IIR == NO_INT */
3211 if (port->type == PORT_TEGRA)
3212 up->bugs |= UART_BUG_NOMSR;
3214 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3217 if (port->type == PORT_UNKNOWN)
3218 serial8250_release_std_resource(up);
3220 register_dev_spec_attr_grp(up);
3221 up->fcr = uart_config[up->port.type].fcr;
3225 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3227 if (ser->irq >= nr_irqs || ser->irq < 0 ||
3228 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3229 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3230 ser->type == PORT_STARTECH)
3235 static const char *serial8250_type(struct uart_port *port)
3237 int type = port->type;
3239 if (type >= ARRAY_SIZE(uart_config))
3241 return uart_config[type].name;
3244 static const struct uart_ops serial8250_pops = {
3245 .tx_empty = serial8250_tx_empty,
3246 .set_mctrl = serial8250_set_mctrl,
3247 .get_mctrl = serial8250_get_mctrl,
3248 .stop_tx = serial8250_stop_tx,
3249 .start_tx = serial8250_start_tx,
3250 .throttle = serial8250_throttle,
3251 .unthrottle = serial8250_unthrottle,
3252 .stop_rx = serial8250_stop_rx,
3253 .enable_ms = serial8250_enable_ms,
3254 .break_ctl = serial8250_break_ctl,
3255 .startup = serial8250_startup,
3256 .shutdown = serial8250_shutdown,
3257 .set_termios = serial8250_set_termios,
3258 .set_ldisc = serial8250_set_ldisc,
3259 .pm = serial8250_pm,
3260 .type = serial8250_type,
3261 .release_port = serial8250_release_port,
3262 .request_port = serial8250_request_port,
3263 .config_port = serial8250_config_port,
3264 .verify_port = serial8250_verify_port,
3265 #ifdef CONFIG_CONSOLE_POLL
3266 .poll_get_char = serial8250_get_poll_char,
3267 .poll_put_char = serial8250_put_poll_char,
3271 void serial8250_init_port(struct uart_8250_port *up)
3273 struct uart_port *port = &up->port;
3275 spin_lock_init(&port->lock);
3278 port->ops = &serial8250_pops;
3279 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
3281 up->cur_iotype = 0xFF;
3283 EXPORT_SYMBOL_GPL(serial8250_init_port);
3285 void serial8250_set_defaults(struct uart_8250_port *up)
3287 struct uart_port *port = &up->port;
3289 if (up->port.flags & UPF_FIXED_TYPE) {
3290 unsigned int type = up->port.type;
3292 if (!up->port.fifosize)
3293 up->port.fifosize = uart_config[type].fifo_size;
3295 up->tx_loadsz = uart_config[type].tx_loadsz;
3296 if (!up->capabilities)
3297 up->capabilities = uart_config[type].flags;
3300 set_io_from_upio(port);
3302 /* default dma handlers */
3304 if (!up->dma->tx_dma)
3305 up->dma->tx_dma = serial8250_tx_dma;
3306 if (!up->dma->rx_dma)
3307 up->dma->rx_dma = serial8250_rx_dma;
3310 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3312 #ifdef CONFIG_SERIAL_8250_CONSOLE
3314 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch)
3316 struct uart_8250_port *up = up_to_u8250p(port);
3318 wait_for_xmitr(up, UART_LSR_THRE);
3319 serial_port_out(port, UART_TX, ch);
3323 * Restore serial console when h/w power-off detected
3325 static void serial8250_console_restore(struct uart_8250_port *up)
3327 struct uart_port *port = &up->port;
3328 struct ktermios termios;
3329 unsigned int baud, quot, frac = 0;
3331 termios.c_cflag = port->cons->cflag;
3332 termios.c_ispeed = port->cons->ispeed;
3333 termios.c_ospeed = port->cons->ospeed;
3334 if (port->state->port.tty && termios.c_cflag == 0) {
3335 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3336 termios.c_ispeed = port->state->port.tty->termios.c_ispeed;
3337 termios.c_ospeed = port->state->port.tty->termios.c_ospeed;
3340 baud = serial8250_get_baud_rate(port, &termios, NULL);
3341 quot = serial8250_get_divisor(port, baud, &frac);
3343 serial8250_set_divisor(port, baud, quot, frac);
3344 serial_port_out(port, UART_LCR, up->lcr);
3345 serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS);
3349 * Print a string to the serial port using the device FIFO
3351 * It sends fifosize bytes and then waits for the fifo
3354 static void serial8250_console_fifo_write(struct uart_8250_port *up,
3355 const char *s, unsigned int count)
3358 const char *end = s + count;
3359 unsigned int fifosize = up->tx_loadsz;
3360 bool cr_sent = false;
3363 wait_for_lsr(up, UART_LSR_THRE);
3365 for (i = 0; i < fifosize && s != end; ++i) {
3366 if (*s == '\n' && !cr_sent) {
3367 serial_out(up, UART_TX, '\r');
3370 serial_out(up, UART_TX, *s++);
3378 * Print a string to the serial port trying not to disturb
3379 * any possible real use of the port...
3381 * The console_lock must be held when we get here.
3383 * Doing runtime PM is really a bad idea for the kernel console.
3384 * Thus, we assume the function is called when device is powered up.
3386 void serial8250_console_write(struct uart_8250_port *up, const char *s,
3389 struct uart_8250_em485 *em485 = up->em485;
3390 struct uart_port *port = &up->port;
3391 unsigned long flags;
3392 unsigned int ier, use_fifo;
3395 touch_nmi_watchdog();
3397 if (oops_in_progress)
3398 locked = spin_trylock_irqsave(&port->lock, flags);
3400 spin_lock_irqsave(&port->lock, flags);
3403 * First save the IER then disable the interrupts
3405 ier = serial_port_in(port, UART_IER);
3406 serial8250_clear_IER(up);
3408 /* check scratch reg to see if port powered off during system sleep */
3409 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3410 serial8250_console_restore(up);
3415 if (em485->tx_stopped)
3416 up->rs485_start_tx(up);
3417 mdelay(port->rs485.delay_rts_before_send);
3420 use_fifo = (up->capabilities & UART_CAP_FIFO) &&
3422 * BCM283x requires to check the fifo
3425 !(up->capabilities & UART_CAP_MINI) &&
3427 * tx_loadsz contains the transmit fifo size
3429 up->tx_loadsz > 1 &&
3430 (up->fcr & UART_FCR_ENABLE_FIFO) &&
3432 test_bit(TTY_PORT_INITIALIZED, &port->state->port.iflags) &&
3434 * After we put a data in the fifo, the controller will send
3435 * it regardless of the CTS state. Therefore, only use fifo
3436 * if we don't use control flow.
3438 !(up->port.flags & UPF_CONS_FLOW);
3440 if (likely(use_fifo))
3441 serial8250_console_fifo_write(up, s, count);
3443 uart_console_write(port, s, count, serial8250_console_putchar);
3446 * Finally, wait for transmitter to become empty
3447 * and restore the IER
3449 wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
3452 mdelay(port->rs485.delay_rts_after_send);
3453 if (em485->tx_stopped)
3454 up->rs485_stop_tx(up);
3457 serial_port_out(port, UART_IER, ier);
3460 * The receive handling will happen properly because the
3461 * receive ready bit will still be set; it is not cleared
3462 * on read. However, modem control will not, we must
3463 * call it if we have saved something in the saved flags
3464 * while processing with interrupts off.
3466 if (up->msr_saved_flags)
3467 serial8250_modem_status(up);
3470 spin_unlock_irqrestore(&port->lock, flags);
3473 static unsigned int probe_baud(struct uart_port *port)
3475 unsigned char lcr, dll, dlm;
3478 lcr = serial_port_in(port, UART_LCR);
3479 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3480 dll = serial_port_in(port, UART_DLL);
3481 dlm = serial_port_in(port, UART_DLM);
3482 serial_port_out(port, UART_LCR, lcr);
3484 quot = (dlm << 8) | dll;
3485 return (port->uartclk / 16) / quot;
3488 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3496 if (!port->iobase && !port->membase)
3500 uart_parse_options(options, &baud, &parity, &bits, &flow);
3502 baud = probe_baud(port);
3504 ret = uart_set_options(port, port->cons, baud, parity, bits, flow);
3509 pm_runtime_get_sync(port->dev);
3514 int serial8250_console_exit(struct uart_port *port)
3517 pm_runtime_put_sync(port->dev);
3522 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3524 MODULE_LICENSE("GPL");