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/console.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/sysrq.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/tty.h>
24 #include <linux/ratelimit.h>
25 #include <linux/tty_flip.h>
26 #include <linux/serial.h>
27 #include <linux/serial_8250.h>
28 #include <linux/nmi.h>
29 #include <linux/mutex.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/ktime.h>
40 /* Nuvoton NPCM timeout register */
41 #define UART_NPCM_TOR 7
42 #define UART_NPCM_TOIE BIT(7) /* Timeout Interrupt Enable */
48 #define DEBUG_AUTOCONF(fmt...) printk(fmt)
50 #define DEBUG_AUTOCONF(fmt...) do { } while (0)
53 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
56 * Here we define the default xmit fifo size used for each type of UART.
58 static const struct serial8250_config uart_config[] = {
83 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
84 .rxtrig_bytes = {1, 4, 8, 14},
85 .flags = UART_CAP_FIFO,
96 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
102 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
104 .rxtrig_bytes = {8, 16, 24, 28},
105 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
111 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
113 .rxtrig_bytes = {1, 16, 32, 56},
114 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE,
122 .name = "16C950/954",
125 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
126 .rxtrig_bytes = {16, 32, 112, 120},
127 /* UART_CAP_EFR breaks billionon CF bluetooth card. */
128 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
134 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
136 .rxtrig_bytes = {8, 16, 56, 60},
137 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
143 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
144 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP,
150 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11,
151 .flags = UART_CAP_FIFO,
157 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
158 .flags = UART_CAP_FIFO | UART_NATSEMI,
164 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
165 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE,
171 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
172 .flags = UART_CAP_FIFO,
178 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
179 .flags = UART_CAP_FIFO /* | UART_CAP_AFE */,
185 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
186 .flags = UART_CAP_FIFO | UART_CAP_AFE,
192 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 |
194 .rxtrig_bytes = {1, 4, 8, 14},
195 .flags = UART_CAP_FIFO | UART_CAP_RTOIE,
201 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
202 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
209 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 |
211 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR |
218 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
219 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00,
220 .flags = UART_CAP_FIFO,
222 [PORT_BRCM_TRUMANAGE] = {
226 .flags = UART_CAP_HFIFO,
231 [PORT_ALTR_16550_F32] = {
232 .name = "Altera 16550 FIFO32",
235 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
236 .rxtrig_bytes = {1, 8, 16, 30},
237 .flags = UART_CAP_FIFO | UART_CAP_AFE,
239 [PORT_ALTR_16550_F64] = {
240 .name = "Altera 16550 FIFO64",
243 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
244 .rxtrig_bytes = {1, 16, 32, 62},
245 .flags = UART_CAP_FIFO | UART_CAP_AFE,
247 [PORT_ALTR_16550_F128] = {
248 .name = "Altera 16550 FIFO128",
251 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
252 .rxtrig_bytes = {1, 32, 64, 126},
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 | UART_CAP_NOTEMT,
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,
277 .name = "TI DA8xx/66AK2x",
280 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO |
282 .rxtrig_bytes = {1, 4, 8, 14},
283 .flags = UART_CAP_FIFO | UART_CAP_AFE,
286 .name = "MediaTek BTIF",
289 .fcr = UART_FCR_ENABLE_FIFO |
290 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
291 .flags = UART_CAP_FIFO,
294 .name = "Nuvoton 16550",
297 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 |
298 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
299 .rxtrig_bytes = {1, 4, 8, 14},
300 .flags = UART_CAP_FIFO,
306 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10,
307 .rxtrig_bytes = {1, 32, 64, 112},
308 .flags = UART_CAP_FIFO | UART_CAP_SLEEP,
310 [PORT_ASPEED_VUART] = {
311 .name = "ASPEED VUART",
314 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00,
315 .rxtrig_bytes = {1, 4, 8, 14},
316 .flags = UART_CAP_FIFO,
320 /* Uart divisor latch read */
321 static int default_serial_dl_read(struct uart_8250_port *up)
323 /* Assign these in pieces to truncate any bits above 7. */
324 unsigned char dll = serial_in(up, UART_DLL);
325 unsigned char dlm = serial_in(up, UART_DLM);
327 return dll | dlm << 8;
330 /* Uart divisor latch write */
331 static void default_serial_dl_write(struct uart_8250_port *up, int value)
333 serial_out(up, UART_DLL, value & 0xff);
334 serial_out(up, UART_DLM, value >> 8 & 0xff);
337 #ifdef CONFIG_SERIAL_8250_RT288X
339 /* Au1x00/RT288x UART hardware has a weird register layout */
340 static const s8 au_io_in_map[8] = {
348 -1, /* UART_SCR (unmapped) */
351 static const s8 au_io_out_map[8] = {
357 -1, /* UART_LSR (unmapped) */
358 -1, /* UART_MSR (unmapped) */
359 -1, /* UART_SCR (unmapped) */
362 unsigned int au_serial_in(struct uart_port *p, int offset)
364 if (offset >= ARRAY_SIZE(au_io_in_map))
366 offset = au_io_in_map[offset];
369 return __raw_readl(p->membase + (offset << p->regshift));
372 void au_serial_out(struct uart_port *p, int offset, int value)
374 if (offset >= ARRAY_SIZE(au_io_out_map))
376 offset = au_io_out_map[offset];
379 __raw_writel(value, p->membase + (offset << p->regshift));
382 /* Au1x00 haven't got a standard divisor latch */
383 static int au_serial_dl_read(struct uart_8250_port *up)
385 return __raw_readl(up->port.membase + 0x28);
388 static void au_serial_dl_write(struct uart_8250_port *up, int value)
390 __raw_writel(value, up->port.membase + 0x28);
395 static unsigned int hub6_serial_in(struct uart_port *p, int offset)
397 offset = offset << p->regshift;
398 outb(p->hub6 - 1 + offset, p->iobase);
399 return inb(p->iobase + 1);
402 static void hub6_serial_out(struct uart_port *p, int offset, int value)
404 offset = offset << p->regshift;
405 outb(p->hub6 - 1 + offset, p->iobase);
406 outb(value, p->iobase + 1);
409 static unsigned int mem_serial_in(struct uart_port *p, int offset)
411 offset = offset << p->regshift;
412 return readb(p->membase + offset);
415 static void mem_serial_out(struct uart_port *p, int offset, int value)
417 offset = offset << p->regshift;
418 writeb(value, p->membase + offset);
421 static void mem16_serial_out(struct uart_port *p, int offset, int value)
423 offset = offset << p->regshift;
424 writew(value, p->membase + offset);
427 static unsigned int mem16_serial_in(struct uart_port *p, int offset)
429 offset = offset << p->regshift;
430 return readw(p->membase + offset);
433 static void mem32_serial_out(struct uart_port *p, int offset, int value)
435 offset = offset << p->regshift;
436 writel(value, p->membase + offset);
439 static unsigned int mem32_serial_in(struct uart_port *p, int offset)
441 offset = offset << p->regshift;
442 return readl(p->membase + offset);
445 static void mem32be_serial_out(struct uart_port *p, int offset, int value)
447 offset = offset << p->regshift;
448 iowrite32be(value, p->membase + offset);
451 static unsigned int mem32be_serial_in(struct uart_port *p, int offset)
453 offset = offset << p->regshift;
454 return ioread32be(p->membase + offset);
457 static unsigned int io_serial_in(struct uart_port *p, int offset)
459 offset = offset << p->regshift;
460 return inb(p->iobase + offset);
463 static void io_serial_out(struct uart_port *p, int offset, int value)
465 offset = offset << p->regshift;
466 outb(value, p->iobase + offset);
469 static int serial8250_default_handle_irq(struct uart_port *port);
471 static void set_io_from_upio(struct uart_port *p)
473 struct uart_8250_port *up = up_to_u8250p(p);
475 up->dl_read = default_serial_dl_read;
476 up->dl_write = default_serial_dl_write;
480 p->serial_in = hub6_serial_in;
481 p->serial_out = hub6_serial_out;
485 p->serial_in = mem_serial_in;
486 p->serial_out = mem_serial_out;
490 p->serial_in = mem16_serial_in;
491 p->serial_out = mem16_serial_out;
495 p->serial_in = mem32_serial_in;
496 p->serial_out = mem32_serial_out;
500 p->serial_in = mem32be_serial_in;
501 p->serial_out = mem32be_serial_out;
504 #ifdef CONFIG_SERIAL_8250_RT288X
506 p->serial_in = au_serial_in;
507 p->serial_out = au_serial_out;
508 up->dl_read = au_serial_dl_read;
509 up->dl_write = au_serial_dl_write;
514 p->serial_in = io_serial_in;
515 p->serial_out = io_serial_out;
518 /* Remember loaded iotype */
519 up->cur_iotype = p->iotype;
520 p->handle_irq = serial8250_default_handle_irq;
524 serial_port_out_sync(struct uart_port *p, int offset, int value)
532 p->serial_out(p, offset, value);
533 p->serial_in(p, UART_LCR); /* safe, no side-effects */
536 p->serial_out(p, offset, value);
543 static void serial8250_clear_fifos(struct uart_8250_port *p)
545 if (p->capabilities & UART_CAP_FIFO) {
546 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO);
547 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO |
548 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
549 serial_out(p, UART_FCR, 0);
553 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t);
554 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t);
556 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p)
558 serial8250_clear_fifos(p);
559 serial_out(p, UART_FCR, p->fcr);
561 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos);
563 void serial8250_rpm_get(struct uart_8250_port *p)
565 if (!(p->capabilities & UART_CAP_RPM))
567 pm_runtime_get_sync(p->port.dev);
569 EXPORT_SYMBOL_GPL(serial8250_rpm_get);
571 void serial8250_rpm_put(struct uart_8250_port *p)
573 if (!(p->capabilities & UART_CAP_RPM))
575 pm_runtime_mark_last_busy(p->port.dev);
576 pm_runtime_put_autosuspend(p->port.dev);
578 EXPORT_SYMBOL_GPL(serial8250_rpm_put);
581 * serial8250_em485_init() - put uart_8250_port into rs485 emulating
582 * @p: uart_8250_port port instance
584 * The function is used to start rs485 software emulating on the
585 * &struct uart_8250_port* @p. Namely, RTS is switched before/after
586 * transmission. The function is idempotent, so it is safe to call it
589 * The caller MUST enable interrupt on empty shift register before
590 * calling serial8250_em485_init(). This interrupt is not a part of
591 * 8250 standard, but implementation defined.
593 * The function is supposed to be called from .rs485_config callback
594 * or from any other callback protected with p->port.lock spinlock.
596 * See also serial8250_em485_destroy()
598 * Return 0 - success, -errno - otherwise
600 static int serial8250_em485_init(struct uart_8250_port *p)
605 p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC);
609 hrtimer_init(&p->em485->stop_tx_timer, CLOCK_MONOTONIC,
611 hrtimer_init(&p->em485->start_tx_timer, CLOCK_MONOTONIC,
613 p->em485->stop_tx_timer.function = &serial8250_em485_handle_stop_tx;
614 p->em485->start_tx_timer.function = &serial8250_em485_handle_start_tx;
616 p->em485->active_timer = NULL;
617 p->em485->tx_stopped = true;
625 * serial8250_em485_destroy() - put uart_8250_port into normal state
626 * @p: uart_8250_port port instance
628 * The function is used to stop rs485 software emulating on the
629 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to
630 * call it multiple times.
632 * The function is supposed to be called from .rs485_config callback
633 * or from any other callback protected with p->port.lock spinlock.
635 * See also serial8250_em485_init()
637 void serial8250_em485_destroy(struct uart_8250_port *p)
642 hrtimer_cancel(&p->em485->start_tx_timer);
643 hrtimer_cancel(&p->em485->stop_tx_timer);
648 EXPORT_SYMBOL_GPL(serial8250_em485_destroy);
651 * serial8250_em485_config() - generic ->rs485_config() callback
653 * @rs485: rs485 settings
655 * Generic callback usable by 8250 uart drivers to activate rs485 settings
656 * if the uart is incapable of driving RTS as a Transmit Enable signal in
657 * hardware, relying on software emulation instead.
659 int serial8250_em485_config(struct uart_port *port, struct serial_rs485 *rs485)
661 struct uart_8250_port *up = up_to_u8250p(port);
663 /* pick sane settings if the user hasn't */
664 if (!!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
665 !!(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
666 rs485->flags |= SER_RS485_RTS_ON_SEND;
667 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
670 /* clamp the delays to [0, 100ms] */
671 rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
672 rs485->delay_rts_after_send = min(rs485->delay_rts_after_send, 100U);
674 memset(rs485->padding, 0, sizeof(rs485->padding));
675 port->rs485 = *rs485;
677 gpiod_set_value(port->rs485_term_gpio,
678 rs485->flags & SER_RS485_TERMINATE_BUS);
681 * Both serial8250_em485_init() and serial8250_em485_destroy()
684 if (rs485->flags & SER_RS485_ENABLED) {
685 int ret = serial8250_em485_init(up);
688 rs485->flags &= ~SER_RS485_ENABLED;
689 port->rs485.flags &= ~SER_RS485_ENABLED;
694 serial8250_em485_destroy(up);
697 EXPORT_SYMBOL_GPL(serial8250_em485_config);
700 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than
701 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is
702 * empty and the HW can idle again.
704 void serial8250_rpm_get_tx(struct uart_8250_port *p)
706 unsigned char rpm_active;
708 if (!(p->capabilities & UART_CAP_RPM))
711 rpm_active = xchg(&p->rpm_tx_active, 1);
714 pm_runtime_get_sync(p->port.dev);
716 EXPORT_SYMBOL_GPL(serial8250_rpm_get_tx);
718 void serial8250_rpm_put_tx(struct uart_8250_port *p)
720 unsigned char rpm_active;
722 if (!(p->capabilities & UART_CAP_RPM))
725 rpm_active = xchg(&p->rpm_tx_active, 0);
728 pm_runtime_mark_last_busy(p->port.dev);
729 pm_runtime_put_autosuspend(p->port.dev);
731 EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx);
734 * IER sleep support. UARTs which have EFRs need the "extended
735 * capability" bit enabled. Note that on XR16C850s, we need to
736 * reset LCR to write to IER.
738 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
740 unsigned char lcr = 0, efr = 0;
742 serial8250_rpm_get(p);
744 if (p->capabilities & UART_CAP_SLEEP) {
745 if (p->capabilities & UART_CAP_EFR) {
746 lcr = serial_in(p, UART_LCR);
747 efr = serial_in(p, UART_EFR);
748 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
749 serial_out(p, UART_EFR, UART_EFR_ECB);
750 serial_out(p, UART_LCR, 0);
752 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0);
753 if (p->capabilities & UART_CAP_EFR) {
754 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B);
755 serial_out(p, UART_EFR, efr);
756 serial_out(p, UART_LCR, lcr);
760 serial8250_rpm_put(p);
763 #ifdef CONFIG_SERIAL_8250_RSA
765 * Attempts to turn on the RSA FIFO. Returns zero on failure.
766 * We set the port uart clock rate if we succeed.
768 static int __enable_rsa(struct uart_8250_port *up)
773 mode = serial_in(up, UART_RSA_MSR);
774 result = mode & UART_RSA_MSR_FIFO;
777 serial_out(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO);
778 mode = serial_in(up, UART_RSA_MSR);
779 result = mode & UART_RSA_MSR_FIFO;
783 up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16;
788 static void enable_rsa(struct uart_8250_port *up)
790 if (up->port.type == PORT_RSA) {
791 if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) {
792 spin_lock_irq(&up->port.lock);
794 spin_unlock_irq(&up->port.lock);
796 if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16)
797 serial_out(up, UART_RSA_FRR, 0);
802 * Attempts to turn off the RSA FIFO. Returns zero on failure.
803 * It is unknown why interrupts were disabled in here. However,
804 * the caller is expected to preserve this behaviour by grabbing
805 * the spinlock before calling this function.
807 static void disable_rsa(struct uart_8250_port *up)
812 if (up->port.type == PORT_RSA &&
813 up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) {
814 spin_lock_irq(&up->port.lock);
816 mode = serial_in(up, UART_RSA_MSR);
817 result = !(mode & UART_RSA_MSR_FIFO);
820 serial_out(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO);
821 mode = serial_in(up, UART_RSA_MSR);
822 result = !(mode & UART_RSA_MSR_FIFO);
826 up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16;
827 spin_unlock_irq(&up->port.lock);
830 #endif /* CONFIG_SERIAL_8250_RSA */
833 * This is a quickie test to see how big the FIFO is.
834 * It doesn't work at all the time, more's the pity.
836 static int size_fifo(struct uart_8250_port *up)
838 unsigned char old_fcr, old_mcr, old_lcr;
839 unsigned short old_dl;
842 old_lcr = serial_in(up, UART_LCR);
843 serial_out(up, UART_LCR, 0);
844 old_fcr = serial_in(up, UART_FCR);
845 old_mcr = serial8250_in_MCR(up);
846 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
847 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
848 serial8250_out_MCR(up, UART_MCR_LOOP);
849 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
850 old_dl = serial_dl_read(up);
851 serial_dl_write(up, 0x0001);
852 serial_out(up, UART_LCR, 0x03);
853 for (count = 0; count < 256; count++)
854 serial_out(up, UART_TX, count);
855 mdelay(20);/* FIXME - schedule_timeout */
856 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) &&
857 (count < 256); count++)
858 serial_in(up, UART_RX);
859 serial_out(up, UART_FCR, old_fcr);
860 serial8250_out_MCR(up, old_mcr);
861 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
862 serial_dl_write(up, old_dl);
863 serial_out(up, UART_LCR, old_lcr);
869 * Read UART ID using the divisor method - set DLL and DLM to zero
870 * and the revision will be in DLL and device type in DLM. We
871 * preserve the device state across this.
873 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p)
875 unsigned char old_lcr;
876 unsigned int id, old_dl;
878 old_lcr = serial_in(p, UART_LCR);
879 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A);
880 old_dl = serial_dl_read(p);
881 serial_dl_write(p, 0);
882 id = serial_dl_read(p);
883 serial_dl_write(p, old_dl);
885 serial_out(p, UART_LCR, old_lcr);
891 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's.
892 * When this function is called we know it is at least a StarTech
893 * 16650 V2, but it might be one of several StarTech UARTs, or one of
894 * its clones. (We treat the broken original StarTech 16650 V1 as a
895 * 16550, and why not? Startech doesn't seem to even acknowledge its
898 * What evil have men's minds wrought...
900 static void autoconfig_has_efr(struct uart_8250_port *up)
902 unsigned int id1, id2, id3, rev;
905 * Everything with an EFR has SLEEP
907 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
910 * First we check to see if it's an Oxford Semiconductor UART.
912 * If we have to do this here because some non-National
913 * Semiconductor clone chips lock up if you try writing to the
914 * LSR register (which serial_icr_read does)
918 * Check for Oxford Semiconductor 16C950.
920 * EFR [4] must be set else this test fails.
922 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca)
923 * claims that it's needed for 952 dual UART's (which are not
924 * recommended for new designs).
927 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
928 serial_out(up, UART_EFR, UART_EFR_ECB);
929 serial_out(up, UART_LCR, 0x00);
930 id1 = serial_icr_read(up, UART_ID1);
931 id2 = serial_icr_read(up, UART_ID2);
932 id3 = serial_icr_read(up, UART_ID3);
933 rev = serial_icr_read(up, UART_REV);
935 DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev);
937 if (id1 == 0x16 && id2 == 0xC9 &&
938 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) {
939 up->port.type = PORT_16C950;
942 * Enable work around for the Oxford Semiconductor 952 rev B
943 * chip which causes it to seriously miscalculate baud rates
946 if (id3 == 0x52 && rev == 0x01)
947 up->bugs |= UART_BUG_QUOT;
952 * We check for a XR16C850 by setting DLL and DLM to 0, and then
953 * reading back DLL and DLM. The chip type depends on the DLM
955 * 0x10 - XR16C850 and the DLL contains the chip revision.
959 id1 = autoconfig_read_divisor_id(up);
960 DEBUG_AUTOCONF("850id=%04x ", id1);
963 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) {
964 up->port.type = PORT_16850;
969 * It wasn't an XR16C850.
971 * We distinguish between the '654 and the '650 by counting
972 * how many bytes are in the FIFO. I'm using this for now,
973 * since that's the technique that was sent to me in the
974 * serial driver update, but I'm not convinced this works.
975 * I've had problems doing this in the past. -TYT
977 if (size_fifo(up) == 64)
978 up->port.type = PORT_16654;
980 up->port.type = PORT_16650V2;
984 * We detected a chip without a FIFO. Only two fall into
985 * this category - the original 8250 and the 16450. The
986 * 16450 has a scratch register (accessible with LCR=0)
988 static void autoconfig_8250(struct uart_8250_port *up)
990 unsigned char scratch, status1, status2;
992 up->port.type = PORT_8250;
994 scratch = serial_in(up, UART_SCR);
995 serial_out(up, UART_SCR, 0xa5);
996 status1 = serial_in(up, UART_SCR);
997 serial_out(up, UART_SCR, 0x5a);
998 status2 = serial_in(up, UART_SCR);
999 serial_out(up, UART_SCR, scratch);
1001 if (status1 == 0xa5 && status2 == 0x5a)
1002 up->port.type = PORT_16450;
1005 static int broken_efr(struct uart_8250_port *up)
1008 * Exar ST16C2550 "A2" devices incorrectly detect as
1009 * having an EFR, and report an ID of 0x0201. See
1010 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html
1012 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16)
1019 * We know that the chip has FIFOs. Does it have an EFR? The
1020 * EFR is located in the same register position as the IIR and
1021 * we know the top two bits of the IIR are currently set. The
1022 * EFR should contain zero. Try to read the EFR.
1024 static void autoconfig_16550a(struct uart_8250_port *up)
1026 unsigned char status1, status2;
1027 unsigned int iersave;
1029 up->port.type = PORT_16550A;
1030 up->capabilities |= UART_CAP_FIFO;
1032 if (!IS_ENABLED(CONFIG_SERIAL_8250_16550A_VARIANTS))
1036 * Check for presence of the EFR when DLAB is set.
1037 * Only ST16C650V1 UARTs pass this test.
1039 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1040 if (serial_in(up, UART_EFR) == 0) {
1041 serial_out(up, UART_EFR, 0xA8);
1042 if (serial_in(up, UART_EFR) != 0) {
1043 DEBUG_AUTOCONF("EFRv1 ");
1044 up->port.type = PORT_16650;
1045 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP;
1047 serial_out(up, UART_LCR, 0);
1048 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
1050 status1 = serial_in(up, UART_IIR) >> 5;
1051 serial_out(up, UART_FCR, 0);
1052 serial_out(up, UART_LCR, 0);
1055 up->port.type = PORT_16550A_FSL64;
1057 DEBUG_AUTOCONF("Motorola 8xxx DUART ");
1059 serial_out(up, UART_EFR, 0);
1064 * Maybe it requires 0xbf to be written to the LCR.
1065 * (other ST16C650V2 UARTs, TI16C752A, etc)
1067 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1068 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) {
1069 DEBUG_AUTOCONF("EFRv2 ");
1070 autoconfig_has_efr(up);
1075 * Check for a National Semiconductor SuperIO chip.
1076 * Attempt to switch to bank 2, read the value of the LOOP bit
1077 * from EXCR1. Switch back to bank 0, change it in MCR. Then
1078 * switch back to bank 2, read it from EXCR1 again and check
1079 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2
1081 serial_out(up, UART_LCR, 0);
1082 status1 = serial8250_in_MCR(up);
1083 serial_out(up, UART_LCR, 0xE0);
1084 status2 = serial_in(up, 0x02); /* EXCR1 */
1086 if (!((status2 ^ status1) & UART_MCR_LOOP)) {
1087 serial_out(up, UART_LCR, 0);
1088 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP);
1089 serial_out(up, UART_LCR, 0xE0);
1090 status2 = serial_in(up, 0x02); /* EXCR1 */
1091 serial_out(up, UART_LCR, 0);
1092 serial8250_out_MCR(up, status1);
1094 if ((status2 ^ status1) & UART_MCR_LOOP) {
1095 unsigned short quot;
1097 serial_out(up, UART_LCR, 0xE0);
1099 quot = serial_dl_read(up);
1102 if (ns16550a_goto_highspeed(up))
1103 serial_dl_write(up, quot);
1105 serial_out(up, UART_LCR, 0);
1107 up->port.uartclk = 921600*16;
1108 up->port.type = PORT_NS16550A;
1109 up->capabilities |= UART_NATSEMI;
1115 * No EFR. Try to detect a TI16750, which only sets bit 5 of
1116 * the IIR when 64 byte FIFO mode is enabled when DLAB is set.
1117 * Try setting it with and without DLAB set. Cheap clones
1118 * set bit 5 without DLAB set.
1120 serial_out(up, UART_LCR, 0);
1121 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1122 status1 = serial_in(up, UART_IIR) >> 5;
1123 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1124 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
1125 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
1126 status2 = serial_in(up, UART_IIR) >> 5;
1127 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1128 serial_out(up, UART_LCR, 0);
1130 DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2);
1132 if (status1 == 6 && status2 == 7) {
1133 up->port.type = PORT_16750;
1134 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP;
1139 * Try writing and reading the UART_IER_UUE bit (b6).
1140 * If it works, this is probably one of the Xscale platform's
1142 * We're going to explicitly set the UUE bit to 0 before
1143 * trying to write and read a 1 just to make sure it's not
1144 * already a 1 and maybe locked there before we even start start.
1146 iersave = serial_in(up, UART_IER);
1147 serial_out(up, UART_IER, iersave & ~UART_IER_UUE);
1148 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) {
1150 * OK it's in a known zero state, try writing and reading
1151 * without disturbing the current state of the other bits.
1153 serial_out(up, UART_IER, iersave | UART_IER_UUE);
1154 if (serial_in(up, UART_IER) & UART_IER_UUE) {
1157 * We'll leave the UART_IER_UUE bit set to 1 (enabled).
1159 DEBUG_AUTOCONF("Xscale ");
1160 up->port.type = PORT_XSCALE;
1161 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE;
1166 * If we got here we couldn't force the IER_UUE bit to 0.
1167 * Log it and continue.
1169 DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 ");
1171 serial_out(up, UART_IER, iersave);
1174 * We distinguish between 16550A and U6 16550A by counting
1175 * how many bytes are in the FIFO.
1177 if (up->port.type == PORT_16550A && size_fifo(up) == 64) {
1178 up->port.type = PORT_U6_16550A;
1179 up->capabilities |= UART_CAP_AFE;
1184 * This routine is called by rs_init() to initialize a specific serial
1185 * port. It determines what type of UART chip this serial port is
1186 * using: 8250, 16450, 16550, 16550A. The important question is
1187 * whether or not this UART is a 16550A or not, since this will
1188 * determine whether or not we can use its FIFO features or not.
1190 static void autoconfig(struct uart_8250_port *up)
1192 unsigned char status1, scratch, scratch2, scratch3;
1193 unsigned char save_lcr, save_mcr;
1194 struct uart_port *port = &up->port;
1195 unsigned long flags;
1196 unsigned int old_capabilities;
1198 if (!port->iobase && !port->mapbase && !port->membase)
1201 DEBUG_AUTOCONF("%s: autoconf (0x%04lx, 0x%p): ",
1202 port->name, port->iobase, port->membase);
1205 * We really do need global IRQs disabled here - we're going to
1206 * be frobbing the chips IRQ enable register to see if it exists.
1208 spin_lock_irqsave(&port->lock, flags);
1210 up->capabilities = 0;
1213 if (!(port->flags & UPF_BUGGY_UART)) {
1215 * Do a simple existence test first; if we fail this,
1216 * there's no point trying anything else.
1218 * 0x80 is used as a nonsense port to prevent against
1219 * false positives due to ISA bus float. The
1220 * assumption is that 0x80 is a non-existent port;
1221 * which should be safe since include/asm/io.h also
1222 * makes this assumption.
1224 * Note: this is safe as long as MCR bit 4 is clear
1225 * and the device is in "PC" mode.
1227 scratch = serial_in(up, UART_IER);
1228 serial_out(up, UART_IER, 0);
1233 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL
1234 * 16C754B) allow only to modify them if an EFR bit is set.
1236 scratch2 = serial_in(up, UART_IER) & 0x0f;
1237 serial_out(up, UART_IER, 0x0F);
1241 scratch3 = serial_in(up, UART_IER) & 0x0f;
1242 serial_out(up, UART_IER, scratch);
1243 if (scratch2 != 0 || scratch3 != 0x0F) {
1245 * We failed; there's nothing here
1247 spin_unlock_irqrestore(&port->lock, flags);
1248 DEBUG_AUTOCONF("IER test failed (%02x, %02x) ",
1249 scratch2, scratch3);
1254 save_mcr = serial8250_in_MCR(up);
1255 save_lcr = serial_in(up, UART_LCR);
1258 * Check to see if a UART is really there. Certain broken
1259 * internal modems based on the Rockwell chipset fail this
1260 * test, because they apparently don't implement the loopback
1261 * test mode. So this test is skipped on the COM 1 through
1262 * COM 4 ports. This *should* be safe, since no board
1263 * manufacturer would be stupid enough to design a board
1264 * that conflicts with COM 1-4 --- we hope!
1266 if (!(port->flags & UPF_SKIP_TEST)) {
1267 serial8250_out_MCR(up, UART_MCR_LOOP | 0x0A);
1268 status1 = serial_in(up, UART_MSR) & 0xF0;
1269 serial8250_out_MCR(up, save_mcr);
1270 if (status1 != 0x90) {
1271 spin_unlock_irqrestore(&port->lock, flags);
1272 DEBUG_AUTOCONF("LOOP test failed (%02x) ",
1279 * We're pretty sure there's a port here. Lets find out what
1280 * type of port it is. The IIR top two bits allows us to find
1281 * out if it's 8250 or 16450, 16550, 16550A or later. This
1282 * determines what we test for next.
1284 * We also initialise the EFR (if any) to zero for later. The
1285 * EFR occupies the same register location as the FCR and IIR.
1287 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
1288 serial_out(up, UART_EFR, 0);
1289 serial_out(up, UART_LCR, 0);
1291 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
1293 /* Assign this as it is to truncate any bits above 7. */
1294 scratch = serial_in(up, UART_IIR);
1296 switch (scratch >> 6) {
1298 autoconfig_8250(up);
1301 port->type = PORT_UNKNOWN;
1304 port->type = PORT_16550;
1307 autoconfig_16550a(up);
1311 #ifdef CONFIG_SERIAL_8250_RSA
1313 * Only probe for RSA ports if we got the region.
1315 if (port->type == PORT_16550A && up->probe & UART_PROBE_RSA &&
1317 port->type = PORT_RSA;
1320 serial_out(up, UART_LCR, save_lcr);
1322 port->fifosize = uart_config[up->port.type].fifo_size;
1323 old_capabilities = up->capabilities;
1324 up->capabilities = uart_config[port->type].flags;
1325 up->tx_loadsz = uart_config[port->type].tx_loadsz;
1327 if (port->type == PORT_UNKNOWN)
1333 #ifdef CONFIG_SERIAL_8250_RSA
1334 if (port->type == PORT_RSA)
1335 serial_out(up, UART_RSA_FRR, 0);
1337 serial8250_out_MCR(up, save_mcr);
1338 serial8250_clear_fifos(up);
1339 serial_in(up, UART_RX);
1340 if (up->capabilities & UART_CAP_UUE)
1341 serial_out(up, UART_IER, UART_IER_UUE);
1343 serial_out(up, UART_IER, 0);
1346 spin_unlock_irqrestore(&port->lock, flags);
1349 * Check if the device is a Fintek F81216A
1351 if (port->type == PORT_16550A && port->iotype == UPIO_PORT)
1352 fintek_8250_probe(up);
1354 if (up->capabilities != old_capabilities) {
1355 dev_warn(port->dev, "detected caps %08x should be %08x\n",
1356 old_capabilities, up->capabilities);
1359 DEBUG_AUTOCONF("iir=%d ", scratch);
1360 DEBUG_AUTOCONF("type=%s\n", uart_config[port->type].name);
1363 static void autoconfig_irq(struct uart_8250_port *up)
1365 struct uart_port *port = &up->port;
1366 unsigned char save_mcr, save_ier;
1367 unsigned char save_ICP = 0;
1368 unsigned int ICP = 0;
1372 if (port->flags & UPF_FOURPORT) {
1373 ICP = (port->iobase & 0xfe0) | 0x1f;
1374 save_ICP = inb_p(ICP);
1379 if (uart_console(port))
1382 /* forget possible initially masked and pending IRQ */
1383 probe_irq_off(probe_irq_on());
1384 save_mcr = serial8250_in_MCR(up);
1385 save_ier = serial_in(up, UART_IER);
1386 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2);
1388 irqs = probe_irq_on();
1389 serial8250_out_MCR(up, 0);
1391 if (port->flags & UPF_FOURPORT) {
1392 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS);
1394 serial8250_out_MCR(up,
1395 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1397 serial_out(up, UART_IER, 0x0f); /* enable all intrs */
1398 serial_in(up, UART_LSR);
1399 serial_in(up, UART_RX);
1400 serial_in(up, UART_IIR);
1401 serial_in(up, UART_MSR);
1402 serial_out(up, UART_TX, 0xFF);
1404 irq = probe_irq_off(irqs);
1406 serial8250_out_MCR(up, save_mcr);
1407 serial_out(up, UART_IER, save_ier);
1409 if (port->flags & UPF_FOURPORT)
1410 outb_p(save_ICP, ICP);
1412 if (uart_console(port))
1415 port->irq = (irq > 0) ? irq : 0;
1418 static void serial8250_stop_rx(struct uart_port *port)
1420 struct uart_8250_port *up = up_to_u8250p(port);
1422 serial8250_rpm_get(up);
1424 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI);
1425 up->port.read_status_mask &= ~UART_LSR_DR;
1426 serial_port_out(port, UART_IER, up->ier);
1428 serial8250_rpm_put(up);
1432 * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback
1433 * @p: uart 8250 port
1435 * Generic callback usable by 8250 uart drivers to stop rs485 transmission.
1437 void serial8250_em485_stop_tx(struct uart_8250_port *p)
1439 unsigned char mcr = serial8250_in_MCR(p);
1441 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
1442 mcr |= UART_MCR_RTS;
1444 mcr &= ~UART_MCR_RTS;
1445 serial8250_out_MCR(p, mcr);
1448 * Empty the RX FIFO, we are not interested in anything
1449 * received during the half-duplex transmission.
1450 * Enable previously disabled RX interrupts.
1452 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
1453 serial8250_clear_and_reinit_fifos(p);
1455 p->ier |= UART_IER_RLSI | UART_IER_RDI;
1456 serial_port_out(&p->port, UART_IER, p->ier);
1459 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx);
1461 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t)
1463 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1465 struct uart_8250_port *p = em485->port;
1466 unsigned long flags;
1468 serial8250_rpm_get(p);
1469 spin_lock_irqsave(&p->port.lock, flags);
1470 if (em485->active_timer == &em485->stop_tx_timer) {
1471 p->rs485_stop_tx(p);
1472 em485->active_timer = NULL;
1473 em485->tx_stopped = true;
1475 spin_unlock_irqrestore(&p->port.lock, flags);
1476 serial8250_rpm_put(p);
1478 return HRTIMER_NORESTART;
1481 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
1483 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
1486 static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay)
1488 struct uart_8250_em485 *em485 = p->em485;
1490 stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC;
1493 * rs485_stop_tx() is going to set RTS according to config
1494 * AND flush RX FIFO if required.
1496 if (stop_delay > 0) {
1497 em485->active_timer = &em485->stop_tx_timer;
1498 hrtimer_start(&em485->stop_tx_timer, ns_to_ktime(stop_delay), HRTIMER_MODE_REL);
1500 p->rs485_stop_tx(p);
1501 em485->active_timer = NULL;
1502 em485->tx_stopped = true;
1506 static inline void __do_stop_tx(struct uart_8250_port *p)
1508 if (serial8250_clear_THRI(p))
1509 serial8250_rpm_put_tx(p);
1512 static inline void __stop_tx(struct uart_8250_port *p)
1514 struct uart_8250_em485 *em485 = p->em485;
1517 unsigned char lsr = serial_in(p, UART_LSR);
1520 p->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1522 if (!(lsr & UART_LSR_THRE))
1525 * To provide required timeing and allow FIFO transfer,
1526 * __stop_tx_rs485() must be called only when both FIFO and
1527 * shift register are empty. The device driver should either
1528 * enable interrupt on TEMT or set UART_CAP_NOTEMT that will
1529 * enlarge stop_tx_timer by the tx time of one frame to cover
1530 * for emptying of the shift register.
1532 if (!(lsr & UART_LSR_TEMT)) {
1533 if (!(p->capabilities & UART_CAP_NOTEMT))
1536 * RTS might get deasserted too early with the normal
1537 * frame timing formula. It seems to suggest THRE might
1538 * get asserted already during tx of the stop bit
1539 * rather than after it is fully sent.
1540 * Roughly estimate 1 extra bit here with / 7.
1542 stop_delay = p->port.frame_time + DIV_ROUND_UP(p->port.frame_time, 7);
1545 __stop_tx_rs485(p, stop_delay);
1550 static void serial8250_stop_tx(struct uart_port *port)
1552 struct uart_8250_port *up = up_to_u8250p(port);
1554 serial8250_rpm_get(up);
1558 * We really want to stop the transmitter from sending.
1560 if (port->type == PORT_16C950) {
1561 up->acr |= UART_ACR_TXDIS;
1562 serial_icr_write(up, UART_ACR, up->acr);
1564 serial8250_rpm_put(up);
1567 static inline void __start_tx(struct uart_port *port)
1569 struct uart_8250_port *up = up_to_u8250p(port);
1571 if (up->dma && !up->dma->tx_dma(up))
1574 if (serial8250_set_THRI(up)) {
1575 if (up->bugs & UART_BUG_TXEN) {
1578 lsr = serial_in(up, UART_LSR);
1579 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1580 if (lsr & UART_LSR_THRE)
1581 serial8250_tx_chars(up);
1586 * Re-enable the transmitter if we disabled it.
1588 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) {
1589 up->acr &= ~UART_ACR_TXDIS;
1590 serial_icr_write(up, UART_ACR, up->acr);
1595 * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback
1596 * @up: uart 8250 port
1598 * Generic callback usable by 8250 uart drivers to start rs485 transmission.
1599 * Assumes that setting the RTS bit in the MCR register means RTS is high.
1600 * (Some chips use inverse semantics.) Further assumes that reception is
1601 * stoppable by disabling the UART_IER_RDI interrupt. (Some chips set the
1602 * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.)
1604 void serial8250_em485_start_tx(struct uart_8250_port *up)
1606 unsigned char mcr = serial8250_in_MCR(up);
1608 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX))
1609 serial8250_stop_rx(&up->port);
1611 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND)
1612 mcr |= UART_MCR_RTS;
1614 mcr &= ~UART_MCR_RTS;
1615 serial8250_out_MCR(up, mcr);
1617 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx);
1619 static inline void start_tx_rs485(struct uart_port *port)
1621 struct uart_8250_port *up = up_to_u8250p(port);
1622 struct uart_8250_em485 *em485 = up->em485;
1625 * While serial8250_em485_handle_stop_tx() is a noop if
1626 * em485->active_timer != &em485->stop_tx_timer, it might happen that
1627 * the timer is still armed and triggers only after the current bunch of
1628 * chars is send and em485->active_timer == &em485->stop_tx_timer again.
1629 * So cancel the timer. There is still a theoretical race condition if
1630 * the timer is already running and only comes around to check for
1631 * em485->active_timer when &em485->stop_tx_timer is armed again.
1633 if (em485->active_timer == &em485->stop_tx_timer)
1634 hrtimer_try_to_cancel(&em485->stop_tx_timer);
1636 em485->active_timer = NULL;
1638 if (em485->tx_stopped) {
1639 em485->tx_stopped = false;
1641 up->rs485_start_tx(up);
1643 if (up->port.rs485.delay_rts_before_send > 0) {
1644 em485->active_timer = &em485->start_tx_timer;
1645 start_hrtimer_ms(&em485->start_tx_timer,
1646 up->port.rs485.delay_rts_before_send);
1654 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t)
1656 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485,
1658 struct uart_8250_port *p = em485->port;
1659 unsigned long flags;
1661 spin_lock_irqsave(&p->port.lock, flags);
1662 if (em485->active_timer == &em485->start_tx_timer) {
1663 __start_tx(&p->port);
1664 em485->active_timer = NULL;
1666 spin_unlock_irqrestore(&p->port.lock, flags);
1668 return HRTIMER_NORESTART;
1671 static void serial8250_start_tx(struct uart_port *port)
1673 struct uart_8250_port *up = up_to_u8250p(port);
1674 struct uart_8250_em485 *em485 = up->em485;
1676 if (!port->x_char && uart_circ_empty(&port->state->xmit))
1679 serial8250_rpm_get_tx(up);
1682 em485->active_timer == &em485->start_tx_timer)
1686 start_tx_rs485(port);
1691 static void serial8250_throttle(struct uart_port *port)
1693 port->throttle(port);
1696 static void serial8250_unthrottle(struct uart_port *port)
1698 port->unthrottle(port);
1701 static void serial8250_disable_ms(struct uart_port *port)
1703 struct uart_8250_port *up = up_to_u8250p(port);
1705 /* no MSR capabilities */
1706 if (up->bugs & UART_BUG_NOMSR)
1709 mctrl_gpio_disable_ms(up->gpios);
1711 up->ier &= ~UART_IER_MSI;
1712 serial_port_out(port, UART_IER, up->ier);
1715 static void serial8250_enable_ms(struct uart_port *port)
1717 struct uart_8250_port *up = up_to_u8250p(port);
1719 /* no MSR capabilities */
1720 if (up->bugs & UART_BUG_NOMSR)
1723 mctrl_gpio_enable_ms(up->gpios);
1725 up->ier |= UART_IER_MSI;
1727 serial8250_rpm_get(up);
1728 serial_port_out(port, UART_IER, up->ier);
1729 serial8250_rpm_put(up);
1732 void serial8250_read_char(struct uart_8250_port *up, unsigned char lsr)
1734 struct uart_port *port = &up->port;
1736 char flag = TTY_NORMAL;
1738 if (likely(lsr & UART_LSR_DR))
1739 ch = serial_in(up, UART_RX);
1742 * Intel 82571 has a Serial Over Lan device that will
1743 * set UART_LSR_BI without setting UART_LSR_DR when
1744 * it receives a break. To avoid reading from the
1745 * receive buffer without UART_LSR_DR bit set, we
1746 * just force the read character to be 0
1752 lsr |= up->lsr_saved_flags;
1753 up->lsr_saved_flags = 0;
1755 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
1756 if (lsr & UART_LSR_BI) {
1757 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
1760 * We do the SysRQ and SAK checking
1761 * here because otherwise the break
1762 * may get masked by ignore_status_mask
1763 * or read_status_mask.
1765 if (uart_handle_break(port))
1767 } else if (lsr & UART_LSR_PE)
1768 port->icount.parity++;
1769 else if (lsr & UART_LSR_FE)
1770 port->icount.frame++;
1771 if (lsr & UART_LSR_OE)
1772 port->icount.overrun++;
1775 * Mask off conditions which should be ignored.
1777 lsr &= port->read_status_mask;
1779 if (lsr & UART_LSR_BI) {
1780 dev_dbg(port->dev, "handling break\n");
1782 } else if (lsr & UART_LSR_PE)
1784 else if (lsr & UART_LSR_FE)
1787 if (uart_prepare_sysrq_char(port, ch))
1790 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag);
1792 EXPORT_SYMBOL_GPL(serial8250_read_char);
1795 * serial8250_rx_chars: processes according to the passed in LSR
1796 * value, and returns the remaining LSR bits not handled
1797 * by this Rx routine.
1799 unsigned char serial8250_rx_chars(struct uart_8250_port *up, unsigned char lsr)
1801 struct uart_port *port = &up->port;
1802 int max_count = 256;
1805 serial8250_read_char(up, lsr);
1806 if (--max_count == 0)
1808 lsr = serial_in(up, UART_LSR);
1809 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
1811 tty_flip_buffer_push(&port->state->port);
1814 EXPORT_SYMBOL_GPL(serial8250_rx_chars);
1816 void serial8250_tx_chars(struct uart_8250_port *up)
1818 struct uart_port *port = &up->port;
1819 struct circ_buf *xmit = &port->state->xmit;
1823 uart_xchar_out(port, UART_TX);
1826 if (uart_tx_stopped(port)) {
1827 serial8250_stop_tx(port);
1830 if (uart_circ_empty(xmit)) {
1835 count = up->tx_loadsz;
1837 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
1838 if (up->bugs & UART_BUG_TXRACE) {
1840 * The Aspeed BMC virtual UARTs have a bug where data
1841 * may get stuck in the BMC's Tx FIFO from bursts of
1842 * writes on the APB interface.
1844 * Delay back-to-back writes by a read cycle to avoid
1845 * stalling the VUART. Read a register that won't have
1846 * side-effects and discard the result.
1848 serial_in(up, UART_SCR);
1850 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1852 if (uart_circ_empty(xmit))
1854 if ((up->capabilities & UART_CAP_HFIFO) &&
1855 (serial_in(up, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY)
1857 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */
1858 if ((up->capabilities & UART_CAP_MINI) &&
1859 !(serial_in(up, UART_LSR) & UART_LSR_THRE))
1861 } while (--count > 0);
1863 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1864 uart_write_wakeup(port);
1867 * With RPM enabled, we have to wait until the FIFO is empty before the
1868 * HW can go idle. So we get here once again with empty FIFO and disable
1869 * the interrupt and RPM in __stop_tx()
1871 if (uart_circ_empty(xmit) && !(up->capabilities & UART_CAP_RPM))
1874 EXPORT_SYMBOL_GPL(serial8250_tx_chars);
1876 /* Caller holds uart port lock */
1877 unsigned int serial8250_modem_status(struct uart_8250_port *up)
1879 struct uart_port *port = &up->port;
1880 unsigned int status = serial_in(up, UART_MSR);
1882 status |= up->msr_saved_flags;
1883 up->msr_saved_flags = 0;
1884 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
1885 port->state != NULL) {
1886 if (status & UART_MSR_TERI)
1888 if (status & UART_MSR_DDSR)
1890 if (status & UART_MSR_DDCD)
1891 uart_handle_dcd_change(port, status & UART_MSR_DCD);
1892 if (status & UART_MSR_DCTS)
1893 uart_handle_cts_change(port, status & UART_MSR_CTS);
1895 wake_up_interruptible(&port->state->port.delta_msr_wait);
1900 EXPORT_SYMBOL_GPL(serial8250_modem_status);
1902 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir)
1904 switch (iir & 0x3f) {
1905 case UART_IIR_RX_TIMEOUT:
1906 serial8250_rx_dma_flush(up);
1911 return up->dma->rx_dma(up);
1915 * This handles the interrupt from one port.
1917 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1919 unsigned char status;
1920 struct uart_8250_port *up = up_to_u8250p(port);
1921 bool skip_rx = false;
1922 unsigned long flags;
1924 if (iir & UART_IIR_NO_INT)
1927 spin_lock_irqsave(&port->lock, flags);
1929 status = serial_port_in(port, UART_LSR);
1932 * If port is stopped and there are no error conditions in the
1933 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer
1934 * overflow. Not servicing, RX FIFO would trigger auto HW flow
1935 * control when FIFO occupancy reaches preset threshold, thus
1936 * halting RX. This only works when auto HW flow control is
1939 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) &&
1940 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) &&
1941 !(port->read_status_mask & UART_LSR_DR))
1944 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) {
1945 if (!up->dma || handle_rx_dma(up, iir))
1946 status = serial8250_rx_chars(up, status);
1948 serial8250_modem_status(up);
1949 if ((status & UART_LSR_THRE) && (up->ier & UART_IER_THRI)) {
1950 if (!up->dma || up->dma->tx_err)
1951 serial8250_tx_chars(up);
1956 uart_unlock_and_check_sysrq_irqrestore(port, flags);
1960 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1962 static int serial8250_default_handle_irq(struct uart_port *port)
1964 struct uart_8250_port *up = up_to_u8250p(port);
1968 serial8250_rpm_get(up);
1970 iir = serial_port_in(port, UART_IIR);
1971 ret = serial8250_handle_irq(port, iir);
1973 serial8250_rpm_put(up);
1978 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1979 * have a programmable TX threshold that triggers the THRE interrupt in
1980 * the IIR register. In this case, the THRE interrupt indicates the FIFO
1981 * has space available. Load it up with tx_loadsz bytes.
1983 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1985 unsigned long flags;
1986 unsigned int iir = serial_port_in(port, UART_IIR);
1988 /* TX Threshold IRQ triggered so load up FIFO */
1989 if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1990 struct uart_8250_port *up = up_to_u8250p(port);
1992 spin_lock_irqsave(&port->lock, flags);
1993 serial8250_tx_chars(up);
1994 spin_unlock_irqrestore(&port->lock, flags);
1997 iir = serial_port_in(port, UART_IIR);
1998 return serial8250_handle_irq(port, iir);
2001 static unsigned int serial8250_tx_empty(struct uart_port *port)
2003 struct uart_8250_port *up = up_to_u8250p(port);
2004 unsigned long flags;
2007 serial8250_rpm_get(up);
2009 spin_lock_irqsave(&port->lock, flags);
2010 lsr = serial_port_in(port, UART_LSR);
2011 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
2012 spin_unlock_irqrestore(&port->lock, flags);
2014 serial8250_rpm_put(up);
2016 return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
2019 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
2021 struct uart_8250_port *up = up_to_u8250p(port);
2022 unsigned int status;
2025 serial8250_rpm_get(up);
2026 status = serial8250_modem_status(up);
2027 serial8250_rpm_put(up);
2029 val = serial8250_MSR_to_TIOCM(status);
2031 return mctrl_gpio_get(up->gpios, &val);
2035 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
2037 static unsigned int serial8250_get_mctrl(struct uart_port *port)
2039 if (port->get_mctrl)
2040 return port->get_mctrl(port);
2041 return serial8250_do_get_mctrl(port);
2044 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
2046 struct uart_8250_port *up = up_to_u8250p(port);
2049 mcr = serial8250_TIOCM_to_MCR(mctrl);
2053 serial8250_out_MCR(up, mcr);
2055 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
2057 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
2059 if (port->set_mctrl)
2060 port->set_mctrl(port, mctrl);
2062 serial8250_do_set_mctrl(port, mctrl);
2065 static void serial8250_break_ctl(struct uart_port *port, int break_state)
2067 struct uart_8250_port *up = up_to_u8250p(port);
2068 unsigned long flags;
2070 serial8250_rpm_get(up);
2071 spin_lock_irqsave(&port->lock, flags);
2072 if (break_state == -1)
2073 up->lcr |= UART_LCR_SBC;
2075 up->lcr &= ~UART_LCR_SBC;
2076 serial_port_out(port, UART_LCR, up->lcr);
2077 spin_unlock_irqrestore(&port->lock, flags);
2078 serial8250_rpm_put(up);
2081 static void wait_for_lsr(struct uart_8250_port *up, int bits)
2083 unsigned int status, tmout = 10000;
2085 /* Wait up to 10ms for the character(s) to be sent. */
2087 status = serial_in(up, UART_LSR);
2089 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
2091 if ((status & bits) == bits)
2096 touch_nmi_watchdog();
2101 * Wait for transmitter & holding register to empty
2103 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2107 wait_for_lsr(up, bits);
2109 /* Wait up to 1s for flow control if necessary */
2110 if (up->port.flags & UPF_CONS_FLOW) {
2111 for (tmout = 1000000; tmout; tmout--) {
2112 unsigned int msr = serial_in(up, UART_MSR);
2113 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2114 if (msr & UART_MSR_CTS)
2117 touch_nmi_watchdog();
2122 #ifdef CONFIG_CONSOLE_POLL
2124 * Console polling routines for writing and reading from the uart while
2125 * in an interrupt or debug context.
2128 static int serial8250_get_poll_char(struct uart_port *port)
2130 struct uart_8250_port *up = up_to_u8250p(port);
2134 serial8250_rpm_get(up);
2136 lsr = serial_port_in(port, UART_LSR);
2138 if (!(lsr & UART_LSR_DR)) {
2139 status = NO_POLL_CHAR;
2143 status = serial_port_in(port, UART_RX);
2145 serial8250_rpm_put(up);
2150 static void serial8250_put_poll_char(struct uart_port *port,
2154 struct uart_8250_port *up = up_to_u8250p(port);
2156 serial8250_rpm_get(up);
2158 * First save the IER then disable the interrupts
2160 ier = serial_port_in(port, UART_IER);
2161 if (up->capabilities & UART_CAP_UUE)
2162 serial_port_out(port, UART_IER, UART_IER_UUE);
2164 serial_port_out(port, UART_IER, 0);
2166 wait_for_xmitr(up, BOTH_EMPTY);
2168 * Send the character out.
2170 serial_port_out(port, UART_TX, c);
2173 * Finally, wait for transmitter to become empty
2174 * and restore the IER
2176 wait_for_xmitr(up, BOTH_EMPTY);
2177 serial_port_out(port, UART_IER, ier);
2178 serial8250_rpm_put(up);
2181 #endif /* CONFIG_CONSOLE_POLL */
2183 int serial8250_do_startup(struct uart_port *port)
2185 struct uart_8250_port *up = up_to_u8250p(port);
2186 unsigned long flags;
2187 unsigned char lsr, iir;
2190 if (!port->fifosize)
2191 port->fifosize = uart_config[port->type].fifo_size;
2193 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2194 if (!up->capabilities)
2195 up->capabilities = uart_config[port->type].flags;
2198 if (port->iotype != up->cur_iotype)
2199 set_io_from_upio(port);
2201 serial8250_rpm_get(up);
2202 if (port->type == PORT_16C950) {
2203 /* Wake up and initialize UART */
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);
2215 if (port->type == PORT_DA830) {
2216 /* Reset the port */
2217 serial_port_out(port, UART_IER, 0);
2218 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0);
2221 /* Enable Tx, Rx and free run mode */
2222 serial_port_out(port, UART_DA830_PWREMU_MGMT,
2223 UART_DA830_PWREMU_MGMT_UTRST |
2224 UART_DA830_PWREMU_MGMT_URRST |
2225 UART_DA830_PWREMU_MGMT_FREE);
2228 if (port->type == PORT_NPCM) {
2230 * Nuvoton calls the scratch register 'UART_TOR' (timeout
2231 * register). Enable it, and set TIOC (timeout interrupt
2232 * comparator) to be 0x20 for correct operation.
2234 serial_port_out(port, UART_NPCM_TOR, UART_NPCM_TOIE | 0x20);
2237 #ifdef CONFIG_SERIAL_8250_RSA
2239 * If this is an RSA port, see if we can kick it up to the
2240 * higher speed clock.
2246 * Clear the FIFO buffers and disable them.
2247 * (they will be reenabled in set_termios())
2249 serial8250_clear_fifos(up);
2252 * Clear the interrupt registers.
2254 serial_port_in(port, UART_LSR);
2255 serial_port_in(port, UART_RX);
2256 serial_port_in(port, UART_IIR);
2257 serial_port_in(port, UART_MSR);
2260 * At this point, there's no way the LSR could still be 0xff;
2261 * if it is, then bail out, because there's likely no UART
2264 if (!(port->flags & UPF_BUGGY_UART) &&
2265 (serial_port_in(port, UART_LSR) == 0xff)) {
2266 dev_info_ratelimited(port->dev, "LSR safety check engaged!\n");
2272 * For a XR16C850, we need to set the trigger levels
2274 if (port->type == PORT_16850) {
2277 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2279 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2280 serial_port_out(port, UART_FCTR,
2281 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2282 serial_port_out(port, UART_TRG, UART_TRG_96);
2283 serial_port_out(port, UART_FCTR,
2284 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2285 serial_port_out(port, UART_TRG, UART_TRG_96);
2287 serial_port_out(port, UART_LCR, 0);
2291 * For the Altera 16550 variants, set TX threshold trigger level.
2293 if (((port->type == PORT_ALTR_16550_F32) ||
2294 (port->type == PORT_ALTR_16550_F64) ||
2295 (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2296 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2297 if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2298 dev_err(port->dev, "TX FIFO Threshold errors, skipping\n");
2300 serial_port_out(port, UART_ALTR_AFR,
2301 UART_ALTR_EN_TXFIFO_LW);
2302 serial_port_out(port, UART_ALTR_TX_LOW,
2303 port->fifosize - up->tx_loadsz);
2304 port->handle_irq = serial8250_tx_threshold_handle_irq;
2308 /* Check if we need to have shared IRQs */
2309 if (port->irq && (up->port.flags & UPF_SHARE_IRQ))
2310 up->port.irqflags |= IRQF_SHARED;
2312 if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) {
2315 if (port->irqflags & IRQF_SHARED)
2316 disable_irq_nosync(port->irq);
2319 * Test for UARTs that do not reassert THRE when the
2320 * transmitter is idle and the interrupt has already
2321 * been cleared. Real 16550s should always reassert
2322 * this interrupt whenever the transmitter is idle and
2323 * the interrupt is enabled. Delays are necessary to
2324 * allow register changes to become visible.
2326 spin_lock_irqsave(&port->lock, flags);
2328 wait_for_xmitr(up, UART_LSR_THRE);
2329 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2330 udelay(1); /* allow THRE to set */
2331 iir1 = serial_port_in(port, UART_IIR);
2332 serial_port_out(port, UART_IER, 0);
2333 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2334 udelay(1); /* allow a working UART time to re-assert THRE */
2335 iir = serial_port_in(port, UART_IIR);
2336 serial_port_out(port, UART_IER, 0);
2338 spin_unlock_irqrestore(&port->lock, flags);
2340 if (port->irqflags & IRQF_SHARED)
2341 enable_irq(port->irq);
2344 * If the interrupt is not reasserted, or we otherwise
2345 * don't trust the iir, setup a timer to kick the UART
2346 * on a regular basis.
2348 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2349 up->port.flags & UPF_BUG_THRE) {
2350 up->bugs |= UART_BUG_THRE;
2354 retval = up->ops->setup_irq(up);
2359 * Now, initialize the UART
2361 serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2363 spin_lock_irqsave(&port->lock, flags);
2364 if (up->port.flags & UPF_FOURPORT) {
2366 up->port.mctrl |= TIOCM_OUT1;
2369 * Most PC uarts need OUT2 raised to enable interrupts.
2372 up->port.mctrl |= TIOCM_OUT2;
2374 serial8250_set_mctrl(port, port->mctrl);
2377 * Serial over Lan (SoL) hack:
2378 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be
2379 * used for Serial Over Lan. Those chips take a longer time than a
2380 * normal serial device to signalize that a transmission data was
2381 * queued. Due to that, the above test generally fails. One solution
2382 * would be to delay the reading of iir. However, this is not
2383 * reliable, since the timeout is variable. So, let's just don't
2384 * test if we receive TX irq. This way, we'll never enable
2387 if (up->port.quirks & UPQ_NO_TXEN_TEST)
2388 goto dont_test_tx_en;
2391 * Do a quick test to see if we receive an interrupt when we enable
2394 serial_port_out(port, UART_IER, UART_IER_THRI);
2395 lsr = serial_port_in(port, UART_LSR);
2396 iir = serial_port_in(port, UART_IIR);
2397 serial_port_out(port, UART_IER, 0);
2399 if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2400 if (!(up->bugs & UART_BUG_TXEN)) {
2401 up->bugs |= UART_BUG_TXEN;
2402 dev_dbg(port->dev, "enabling bad tx status workarounds\n");
2405 up->bugs &= ~UART_BUG_TXEN;
2409 spin_unlock_irqrestore(&port->lock, flags);
2412 * Clear the interrupt registers again for luck, and clear the
2413 * saved flags to avoid getting false values from polling
2414 * routines or the previous session.
2416 serial_port_in(port, UART_LSR);
2417 serial_port_in(port, UART_RX);
2418 serial_port_in(port, UART_IIR);
2419 serial_port_in(port, UART_MSR);
2420 up->lsr_saved_flags = 0;
2421 up->msr_saved_flags = 0;
2424 * Request DMA channels for both RX and TX.
2427 const char *msg = NULL;
2429 if (uart_console(port))
2430 msg = "forbid DMA for kernel console";
2431 else if (serial8250_request_dma(up))
2432 msg = "failed to request DMA";
2434 dev_warn_ratelimited(port->dev, "%s\n", msg);
2440 * Set the IER shadow for rx interrupts but defer actual interrupt
2441 * enable until after the FIFOs are enabled; otherwise, an already-
2442 * active sender can swamp the interrupt handler with "too much work".
2444 up->ier = UART_IER_RLSI | UART_IER_RDI;
2446 if (port->flags & UPF_FOURPORT) {
2449 * Enable interrupts on the AST Fourport board
2451 icp = (port->iobase & 0xfe0) | 0x01f;
2457 serial8250_rpm_put(up);
2460 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2462 static int serial8250_startup(struct uart_port *port)
2465 return port->startup(port);
2466 return serial8250_do_startup(port);
2469 void serial8250_do_shutdown(struct uart_port *port)
2471 struct uart_8250_port *up = up_to_u8250p(port);
2472 unsigned long flags;
2474 serial8250_rpm_get(up);
2476 * Disable interrupts from this port
2478 spin_lock_irqsave(&port->lock, flags);
2480 serial_port_out(port, UART_IER, 0);
2481 spin_unlock_irqrestore(&port->lock, flags);
2483 synchronize_irq(port->irq);
2486 serial8250_release_dma(up);
2488 spin_lock_irqsave(&port->lock, flags);
2489 if (port->flags & UPF_FOURPORT) {
2490 /* reset interrupts on the AST Fourport board */
2491 inb((port->iobase & 0xfe0) | 0x1f);
2492 port->mctrl |= TIOCM_OUT1;
2494 port->mctrl &= ~TIOCM_OUT2;
2496 serial8250_set_mctrl(port, port->mctrl);
2497 spin_unlock_irqrestore(&port->lock, flags);
2500 * Disable break condition and FIFOs
2502 serial_port_out(port, UART_LCR,
2503 serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2504 serial8250_clear_fifos(up);
2506 #ifdef CONFIG_SERIAL_8250_RSA
2508 * Reset the RSA board back to 115kbps compat mode.
2514 * Read data port to reset things, and then unlink from
2517 serial_port_in(port, UART_RX);
2518 serial8250_rpm_put(up);
2520 up->ops->release_irq(up);
2522 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2524 static void serial8250_shutdown(struct uart_port *port)
2527 port->shutdown(port);
2529 serial8250_do_shutdown(port);
2532 /* Nuvoton NPCM UARTs have a custom divisor calculation */
2533 static unsigned int npcm_get_divisor(struct uart_8250_port *up,
2536 struct uart_port *port = &up->port;
2538 return DIV_ROUND_CLOSEST(port->uartclk, 16 * baud + 2) - 2;
2541 static unsigned int serial8250_do_get_divisor(struct uart_port *port,
2545 upf_t magic_multiplier = port->flags & UPF_MAGIC_MULTIPLIER;
2546 struct uart_8250_port *up = up_to_u8250p(port);
2550 * Handle magic divisors for baud rates above baud_base on SMSC
2551 * Super I/O chips. We clamp custom rates from clk/6 and clk/12
2552 * up to clk/4 (0x8001) and clk/8 (0x8002) respectively. These
2553 * magic divisors actually reprogram the baud rate generator's
2554 * reference clock derived from chips's 14.318MHz clock input.
2556 * Documentation claims that with these magic divisors the base
2557 * frequencies of 7.3728MHz and 3.6864MHz are used respectively
2558 * for the extra baud rates of 460800bps and 230400bps rather
2559 * than the usual base frequency of 1.8462MHz. However empirical
2560 * evidence contradicts that.
2562 * Instead bit 7 of the DLM register (bit 15 of the divisor) is
2563 * effectively used as a clock prescaler selection bit for the
2564 * base frequency of 7.3728MHz, always used. If set to 0, then
2565 * the base frequency is divided by 4 for use by the Baud Rate
2566 * Generator, for the usual arrangement where the value of 1 of
2567 * the divisor produces the baud rate of 115200bps. Conversely,
2568 * if set to 1 and high-speed operation has been enabled with the
2569 * Serial Port Mode Register in the Device Configuration Space,
2570 * then the base frequency is supplied directly to the Baud Rate
2571 * Generator, so for the divisor values of 0x8001, 0x8002, 0x8003,
2572 * 0x8004, etc. the respective baud rates produced are 460800bps,
2573 * 230400bps, 153600bps, 115200bps, etc.
2575 * In all cases only low 15 bits of the divisor are used to divide
2576 * the baud base and therefore 32767 is the maximum divisor value
2577 * possible, even though documentation says that the programmable
2578 * Baud Rate Generator is capable of dividing the internal PLL
2579 * clock by any divisor from 1 to 65535.
2581 if (magic_multiplier && baud >= port->uartclk / 6)
2583 else if (magic_multiplier && baud >= port->uartclk / 12)
2585 else if (up->port.type == PORT_NPCM)
2586 quot = npcm_get_divisor(up, baud);
2588 quot = uart_get_divisor(port, baud);
2591 * Oxford Semi 952 rev B workaround
2593 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2599 static unsigned int serial8250_get_divisor(struct uart_port *port,
2603 if (port->get_divisor)
2604 return port->get_divisor(port, baud, frac);
2606 return serial8250_do_get_divisor(port, baud, frac);
2609 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2614 cval = UART_LCR_WLEN(tty_get_char_size(c_cflag));
2616 if (c_cflag & CSTOPB)
2617 cval |= UART_LCR_STOP;
2618 if (c_cflag & PARENB) {
2619 cval |= UART_LCR_PARITY;
2620 if (up->bugs & UART_BUG_PARITY)
2621 up->fifo_bug = true;
2623 if (!(c_cflag & PARODD))
2624 cval |= UART_LCR_EPAR;
2625 if (c_cflag & CMSPAR)
2626 cval |= UART_LCR_SPAR;
2631 void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud,
2632 unsigned int quot, unsigned int quot_frac)
2634 struct uart_8250_port *up = up_to_u8250p(port);
2636 /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2637 if (is_omap1510_8250(up)) {
2638 if (baud == 115200) {
2640 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2642 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2646 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2647 * otherwise just set DLAB
2649 if (up->capabilities & UART_NATSEMI)
2650 serial_port_out(port, UART_LCR, 0xe0);
2652 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2654 serial_dl_write(up, quot);
2656 EXPORT_SYMBOL_GPL(serial8250_do_set_divisor);
2658 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2659 unsigned int quot, unsigned int quot_frac)
2661 if (port->set_divisor)
2662 port->set_divisor(port, baud, quot, quot_frac);
2664 serial8250_do_set_divisor(port, baud, quot, quot_frac);
2667 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2668 struct ktermios *termios,
2669 struct ktermios *old)
2671 unsigned int tolerance = port->uartclk / 100;
2676 * Handle magic divisors for baud rates above baud_base on SMSC
2677 * Super I/O chips. Enable custom rates of clk/4 and clk/8, but
2678 * disable divisor values beyond 32767, which are unavailable.
2680 if (port->flags & UPF_MAGIC_MULTIPLIER) {
2681 min = port->uartclk / 16 / UART_DIV_MAX >> 1;
2682 max = (port->uartclk + tolerance) / 4;
2684 min = port->uartclk / 16 / UART_DIV_MAX;
2685 max = (port->uartclk + tolerance) / 16;
2689 * Ask the core to calculate the divisor for us.
2690 * Allow 1% tolerance at the upper limit so uart clks marginally
2691 * slower than nominal still match standard baud rates without
2692 * causing transmission errors.
2694 return uart_get_baud_rate(port, termios, old, min, max);
2698 * Note in order to avoid the tty port mutex deadlock don't use the next method
2699 * within the uart port callbacks. Primarily it's supposed to be utilized to
2700 * handle a sudden reference clock rate change.
2702 void serial8250_update_uartclk(struct uart_port *port, unsigned int uartclk)
2704 struct uart_8250_port *up = up_to_u8250p(port);
2705 struct tty_port *tport = &port->state->port;
2706 unsigned int baud, quot, frac = 0;
2707 struct ktermios *termios;
2708 struct tty_struct *tty;
2709 unsigned long flags;
2711 tty = tty_port_tty_get(tport);
2713 mutex_lock(&tport->mutex);
2714 port->uartclk = uartclk;
2715 mutex_unlock(&tport->mutex);
2719 down_write(&tty->termios_rwsem);
2720 mutex_lock(&tport->mutex);
2722 if (port->uartclk == uartclk)
2725 port->uartclk = uartclk;
2727 if (!tty_port_initialized(tport))
2730 termios = &tty->termios;
2732 baud = serial8250_get_baud_rate(port, termios, NULL);
2733 quot = serial8250_get_divisor(port, baud, &frac);
2735 serial8250_rpm_get(up);
2736 spin_lock_irqsave(&port->lock, flags);
2738 uart_update_timeout(port, termios->c_cflag, baud);
2740 serial8250_set_divisor(port, baud, quot, frac);
2741 serial_port_out(port, UART_LCR, up->lcr);
2743 spin_unlock_irqrestore(&port->lock, flags);
2744 serial8250_rpm_put(up);
2747 mutex_unlock(&tport->mutex);
2748 up_write(&tty->termios_rwsem);
2751 EXPORT_SYMBOL_GPL(serial8250_update_uartclk);
2754 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2755 struct ktermios *old)
2757 struct uart_8250_port *up = up_to_u8250p(port);
2759 unsigned long flags;
2760 unsigned int baud, quot, frac = 0;
2762 if (up->capabilities & UART_CAP_MINI) {
2763 termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR);
2764 if ((termios->c_cflag & CSIZE) == CS5 ||
2765 (termios->c_cflag & CSIZE) == CS6)
2766 termios->c_cflag = (termios->c_cflag & ~CSIZE) | CS7;
2768 cval = serial8250_compute_lcr(up, termios->c_cflag);
2770 baud = serial8250_get_baud_rate(port, termios, old);
2771 quot = serial8250_get_divisor(port, baud, &frac);
2774 * Ok, we're now changing the port state. Do it with
2775 * interrupts disabled.
2777 serial8250_rpm_get(up);
2778 spin_lock_irqsave(&port->lock, flags);
2780 up->lcr = cval; /* Save computed LCR */
2782 if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2783 /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2784 if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2785 up->fcr &= ~UART_FCR_TRIGGER_MASK;
2786 up->fcr |= UART_FCR_TRIGGER_1;
2791 * MCR-based auto flow control. When AFE is enabled, RTS will be
2792 * deasserted when the receive FIFO contains more characters than
2793 * the trigger, or the MCR RTS bit is cleared.
2795 if (up->capabilities & UART_CAP_AFE) {
2796 up->mcr &= ~UART_MCR_AFE;
2797 if (termios->c_cflag & CRTSCTS)
2798 up->mcr |= UART_MCR_AFE;
2802 * Update the per-port timeout.
2804 uart_update_timeout(port, termios->c_cflag, baud);
2806 port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2807 if (termios->c_iflag & INPCK)
2808 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2809 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2810 port->read_status_mask |= UART_LSR_BI;
2813 * Characteres to ignore
2815 port->ignore_status_mask = 0;
2816 if (termios->c_iflag & IGNPAR)
2817 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2818 if (termios->c_iflag & IGNBRK) {
2819 port->ignore_status_mask |= UART_LSR_BI;
2821 * If we're ignoring parity and break indicators,
2822 * ignore overruns too (for real raw support).
2824 if (termios->c_iflag & IGNPAR)
2825 port->ignore_status_mask |= UART_LSR_OE;
2829 * ignore all characters if CREAD is not set
2831 if ((termios->c_cflag & CREAD) == 0)
2832 port->ignore_status_mask |= UART_LSR_DR;
2835 * CTS flow control flag and modem status interrupts
2837 up->ier &= ~UART_IER_MSI;
2838 if (!(up->bugs & UART_BUG_NOMSR) &&
2839 UART_ENABLE_MS(&up->port, termios->c_cflag))
2840 up->ier |= UART_IER_MSI;
2841 if (up->capabilities & UART_CAP_UUE)
2842 up->ier |= UART_IER_UUE;
2843 if (up->capabilities & UART_CAP_RTOIE)
2844 up->ier |= UART_IER_RTOIE;
2846 serial_port_out(port, UART_IER, up->ier);
2848 if (up->capabilities & UART_CAP_EFR) {
2849 unsigned char efr = 0;
2851 * TI16C752/Startech hardware flow control. FIXME:
2852 * - TI16C752 requires control thresholds to be set.
2853 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2855 if (termios->c_cflag & CRTSCTS)
2856 efr |= UART_EFR_CTS;
2858 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2859 if (port->flags & UPF_EXAR_EFR)
2860 serial_port_out(port, UART_XR_EFR, efr);
2862 serial_port_out(port, UART_EFR, efr);
2865 serial8250_set_divisor(port, baud, quot, frac);
2868 * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2869 * is written without DLAB set, this mode will be disabled.
2871 if (port->type == PORT_16750)
2872 serial_port_out(port, UART_FCR, up->fcr);
2874 serial_port_out(port, UART_LCR, up->lcr); /* reset DLAB */
2875 if (port->type != PORT_16750) {
2876 /* emulated UARTs (Lucent Venus 167x) need two steps */
2877 if (up->fcr & UART_FCR_ENABLE_FIFO)
2878 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2879 serial_port_out(port, UART_FCR, up->fcr); /* set fcr */
2881 serial8250_set_mctrl(port, port->mctrl);
2882 spin_unlock_irqrestore(&port->lock, flags);
2883 serial8250_rpm_put(up);
2885 /* Don't rewrite B0 */
2886 if (tty_termios_baud_rate(termios))
2887 tty_termios_encode_baud_rate(termios, baud, baud);
2889 EXPORT_SYMBOL(serial8250_do_set_termios);
2892 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2893 struct ktermios *old)
2895 if (port->set_termios)
2896 port->set_termios(port, termios, old);
2898 serial8250_do_set_termios(port, termios, old);
2901 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios)
2903 if (termios->c_line == N_PPS) {
2904 port->flags |= UPF_HARDPPS_CD;
2905 spin_lock_irq(&port->lock);
2906 serial8250_enable_ms(port);
2907 spin_unlock_irq(&port->lock);
2909 port->flags &= ~UPF_HARDPPS_CD;
2910 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2911 spin_lock_irq(&port->lock);
2912 serial8250_disable_ms(port);
2913 spin_unlock_irq(&port->lock);
2917 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc);
2920 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2922 if (port->set_ldisc)
2923 port->set_ldisc(port, termios);
2925 serial8250_do_set_ldisc(port, termios);
2928 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2929 unsigned int oldstate)
2931 struct uart_8250_port *p = up_to_u8250p(port);
2933 serial8250_set_sleep(p, state != 0);
2935 EXPORT_SYMBOL(serial8250_do_pm);
2938 serial8250_pm(struct uart_port *port, unsigned int state,
2939 unsigned int oldstate)
2942 port->pm(port, state, oldstate);
2944 serial8250_do_pm(port, state, oldstate);
2947 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2949 if (pt->port.mapsize)
2950 return pt->port.mapsize;
2951 if (pt->port.iotype == UPIO_AU) {
2952 if (pt->port.type == PORT_RT2880)
2956 if (is_omap1_8250(pt))
2957 return 0x16 << pt->port.regshift;
2959 return 8 << pt->port.regshift;
2963 * Resource handling.
2965 static int serial8250_request_std_resource(struct uart_8250_port *up)
2967 unsigned int size = serial8250_port_size(up);
2968 struct uart_port *port = &up->port;
2971 switch (port->iotype) {
2981 if (!request_mem_region(port->mapbase, size, "serial")) {
2986 if (port->flags & UPF_IOREMAP) {
2987 port->membase = ioremap(port->mapbase, size);
2988 if (!port->membase) {
2989 release_mem_region(port->mapbase, size);
2997 if (!request_region(port->iobase, size, "serial"))
3004 static void serial8250_release_std_resource(struct uart_8250_port *up)
3006 unsigned int size = serial8250_port_size(up);
3007 struct uart_port *port = &up->port;
3009 switch (port->iotype) {
3019 if (port->flags & UPF_IOREMAP) {
3020 iounmap(port->membase);
3021 port->membase = NULL;
3024 release_mem_region(port->mapbase, size);
3029 release_region(port->iobase, size);
3034 static void serial8250_release_port(struct uart_port *port)
3036 struct uart_8250_port *up = up_to_u8250p(port);
3038 serial8250_release_std_resource(up);
3041 static int serial8250_request_port(struct uart_port *port)
3043 struct uart_8250_port *up = up_to_u8250p(port);
3045 return serial8250_request_std_resource(up);
3048 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
3050 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3051 unsigned char bytes;
3053 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
3055 return bytes ? bytes : -EOPNOTSUPP;
3058 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
3060 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3063 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
3066 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
3067 if (bytes < conf_type->rxtrig_bytes[i])
3068 /* Use the nearest lower value */
3069 return (--i) << UART_FCR_R_TRIG_SHIFT;
3072 return UART_FCR_R_TRIG_11;
3075 static int do_get_rxtrig(struct tty_port *port)
3077 struct uart_state *state = container_of(port, struct uart_state, port);
3078 struct uart_port *uport = state->uart_port;
3079 struct uart_8250_port *up = up_to_u8250p(uport);
3081 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
3084 return fcr_get_rxtrig_bytes(up);
3087 static int do_serial8250_get_rxtrig(struct tty_port *port)
3091 mutex_lock(&port->mutex);
3092 rxtrig_bytes = do_get_rxtrig(port);
3093 mutex_unlock(&port->mutex);
3095 return rxtrig_bytes;
3098 static ssize_t rx_trig_bytes_show(struct device *dev,
3099 struct device_attribute *attr, char *buf)
3101 struct tty_port *port = dev_get_drvdata(dev);
3104 rxtrig_bytes = do_serial8250_get_rxtrig(port);
3105 if (rxtrig_bytes < 0)
3106 return rxtrig_bytes;
3108 return sysfs_emit(buf, "%d\n", rxtrig_bytes);
3111 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
3113 struct uart_state *state = container_of(port, struct uart_state, port);
3114 struct uart_port *uport = state->uart_port;
3115 struct uart_8250_port *up = up_to_u8250p(uport);
3118 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
3122 rxtrig = bytes_to_fcr_rxtrig(up, bytes);
3126 serial8250_clear_fifos(up);
3127 up->fcr &= ~UART_FCR_TRIGGER_MASK;
3128 up->fcr |= (unsigned char)rxtrig;
3129 serial_out(up, UART_FCR, up->fcr);
3133 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
3137 mutex_lock(&port->mutex);
3138 ret = do_set_rxtrig(port, bytes);
3139 mutex_unlock(&port->mutex);
3144 static ssize_t rx_trig_bytes_store(struct device *dev,
3145 struct device_attribute *attr, const char *buf, size_t count)
3147 struct tty_port *port = dev_get_drvdata(dev);
3148 unsigned char bytes;
3154 ret = kstrtou8(buf, 10, &bytes);
3158 ret = do_serial8250_set_rxtrig(port, bytes);
3165 static DEVICE_ATTR_RW(rx_trig_bytes);
3167 static struct attribute *serial8250_dev_attrs[] = {
3168 &dev_attr_rx_trig_bytes.attr,
3172 static struct attribute_group serial8250_dev_attr_group = {
3173 .attrs = serial8250_dev_attrs,
3176 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
3178 const struct serial8250_config *conf_type = &uart_config[up->port.type];
3180 if (conf_type->rxtrig_bytes[0])
3181 up->port.attr_group = &serial8250_dev_attr_group;
3184 static void serial8250_config_port(struct uart_port *port, int flags)
3186 struct uart_8250_port *up = up_to_u8250p(port);
3190 * Find the region that we can probe for. This in turn
3191 * tells us whether we can probe for the type of port.
3193 ret = serial8250_request_std_resource(up);
3197 if (port->iotype != up->cur_iotype)
3198 set_io_from_upio(port);
3200 if (flags & UART_CONFIG_TYPE)
3203 if (port->rs485.flags & SER_RS485_ENABLED)
3204 port->rs485_config(port, &port->rs485);
3206 /* if access method is AU, it is a 16550 with a quirk */
3207 if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3208 up->bugs |= UART_BUG_NOMSR;
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);
3276 port->ops = &serial8250_pops;
3277 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
3279 up->cur_iotype = 0xFF;
3281 EXPORT_SYMBOL_GPL(serial8250_init_port);
3283 void serial8250_set_defaults(struct uart_8250_port *up)
3285 struct uart_port *port = &up->port;
3287 if (up->port.flags & UPF_FIXED_TYPE) {
3288 unsigned int type = up->port.type;
3290 if (!up->port.fifosize)
3291 up->port.fifosize = uart_config[type].fifo_size;
3293 up->tx_loadsz = uart_config[type].tx_loadsz;
3294 if (!up->capabilities)
3295 up->capabilities = uart_config[type].flags;
3298 set_io_from_upio(port);
3300 /* default dma handlers */
3302 if (!up->dma->tx_dma)
3303 up->dma->tx_dma = serial8250_tx_dma;
3304 if (!up->dma->rx_dma)
3305 up->dma->rx_dma = serial8250_rx_dma;
3308 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3310 #ifdef CONFIG_SERIAL_8250_CONSOLE
3312 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch)
3314 struct uart_8250_port *up = up_to_u8250p(port);
3316 wait_for_xmitr(up, UART_LSR_THRE);
3317 serial_port_out(port, UART_TX, ch);
3321 * Restore serial console when h/w power-off detected
3323 static void serial8250_console_restore(struct uart_8250_port *up)
3325 struct uart_port *port = &up->port;
3326 struct ktermios termios;
3327 unsigned int baud, quot, frac = 0;
3329 termios.c_cflag = port->cons->cflag;
3330 if (port->state->port.tty && termios.c_cflag == 0)
3331 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3333 baud = serial8250_get_baud_rate(port, &termios, NULL);
3334 quot = serial8250_get_divisor(port, baud, &frac);
3336 serial8250_set_divisor(port, baud, quot, frac);
3337 serial_port_out(port, UART_LCR, up->lcr);
3338 serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS);
3342 * Print a string to the serial port using the device FIFO
3344 * It sends fifosize bytes and then waits for the fifo
3347 static void serial8250_console_fifo_write(struct uart_8250_port *up,
3348 const char *s, unsigned int count)
3351 const char *end = s + count;
3352 unsigned int fifosize = up->tx_loadsz;
3353 bool cr_sent = false;
3356 wait_for_lsr(up, UART_LSR_THRE);
3358 for (i = 0; i < fifosize && s != end; ++i) {
3359 if (*s == '\n' && !cr_sent) {
3360 serial_out(up, UART_TX, '\r');
3363 serial_out(up, UART_TX, *s++);
3371 * Print a string to the serial port trying not to disturb
3372 * any possible real use of the port...
3374 * The console_lock must be held when we get here.
3376 * Doing runtime PM is really a bad idea for the kernel console.
3377 * Thus, we assume the function is called when device is powered up.
3379 void serial8250_console_write(struct uart_8250_port *up, const char *s,
3382 struct uart_8250_em485 *em485 = up->em485;
3383 struct uart_port *port = &up->port;
3384 unsigned long flags;
3385 unsigned int ier, use_fifo;
3388 touch_nmi_watchdog();
3390 if (oops_in_progress)
3391 locked = spin_trylock_irqsave(&port->lock, flags);
3393 spin_lock_irqsave(&port->lock, flags);
3396 * First save the IER then disable the interrupts
3398 ier = serial_port_in(port, UART_IER);
3400 if (up->capabilities & UART_CAP_UUE)
3401 serial_port_out(port, UART_IER, UART_IER_UUE);
3403 serial_port_out(port, UART_IER, 0);
3405 /* check scratch reg to see if port powered off during system sleep */
3406 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3407 serial8250_console_restore(up);
3412 if (em485->tx_stopped)
3413 up->rs485_start_tx(up);
3414 mdelay(port->rs485.delay_rts_before_send);
3417 use_fifo = (up->capabilities & UART_CAP_FIFO) &&
3419 * BCM283x requires to check the fifo
3422 !(up->capabilities & UART_CAP_MINI) &&
3424 * tx_loadsz contains the transmit fifo size
3426 up->tx_loadsz > 1 &&
3427 (up->fcr & UART_FCR_ENABLE_FIFO) &&
3429 test_bit(TTY_PORT_INITIALIZED, &port->state->port.iflags) &&
3431 * After we put a data in the fifo, the controller will send
3432 * it regardless of the CTS state. Therefore, only use fifo
3433 * if we don't use control flow.
3435 !(up->port.flags & UPF_CONS_FLOW);
3437 if (likely(use_fifo))
3438 serial8250_console_fifo_write(up, s, count);
3440 uart_console_write(port, s, count, serial8250_console_putchar);
3443 * Finally, wait for transmitter to become empty
3444 * and restore the IER
3446 wait_for_xmitr(up, BOTH_EMPTY);
3449 mdelay(port->rs485.delay_rts_after_send);
3450 if (em485->tx_stopped)
3451 up->rs485_stop_tx(up);
3454 serial_port_out(port, UART_IER, ier);
3457 * The receive handling will happen properly because the
3458 * receive ready bit will still be set; it is not cleared
3459 * on read. However, modem control will not, we must
3460 * call it if we have saved something in the saved flags
3461 * while processing with interrupts off.
3463 if (up->msr_saved_flags)
3464 serial8250_modem_status(up);
3467 spin_unlock_irqrestore(&port->lock, flags);
3470 static unsigned int probe_baud(struct uart_port *port)
3472 unsigned char lcr, dll, dlm;
3475 lcr = serial_port_in(port, UART_LCR);
3476 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3477 dll = serial_port_in(port, UART_DLL);
3478 dlm = serial_port_in(port, UART_DLM);
3479 serial_port_out(port, UART_LCR, lcr);
3481 quot = (dlm << 8) | dll;
3482 return (port->uartclk / 16) / quot;
3485 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3493 if (!port->iobase && !port->membase)
3497 uart_parse_options(options, &baud, &parity, &bits, &flow);
3499 baud = probe_baud(port);
3501 ret = uart_set_options(port, port->cons, baud, parity, bits, flow);
3506 pm_runtime_get_sync(port->dev);
3511 int serial8250_console_exit(struct uart_port *port)
3514 pm_runtime_put_sync(port->dev);
3519 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3521 MODULE_LICENSE("GPL");