Lines Matching +full:mux +full:- +full:int +full:- +full:port
1 // SPDX-License-Identifier: GPL-2.0-only
4 * 8250-core based driver for Broadcom ns16550a UARTs
8 * mux for more accurate high speed baud rate selection and also
20 #include <linux/dma-mapping.h>
158 #define UDMA_RX_BUFS_REG_OFFSET (UDMA_RX_BUF1_PTR_LO - UDMA_RX_BUF0_PTR_LO)
170 #define UDMA_TX_BUFS_REG_OFFSET (UDMA_TX_BUF1_PTR_LO - UDMA_TX_BUF0_PTR_LO)
207 int line;
222 int rx_next_buf;
248 int reg_type, int offset) in udma_readl()
250 return readl(priv->regs[reg_type] + offset); in udma_readl()
254 int reg_type, int offset, u32 value) in udma_writel()
256 writel(value, priv->regs[reg_type] + offset); in udma_writel()
260 int reg_type, int offset, u32 bits) in udma_set()
262 void __iomem *reg = priv->regs[reg_type] + offset; in udma_set()
271 int reg_type, int offset, u32 bits) in udma_unset()
273 void __iomem *reg = priv->regs[reg_type] + offset; in udma_unset()
287 static int brcmuart_arbitration(struct brcmuart_priv *priv, bool acquire) in brcmuart_arbitration()
291 int waits; in brcmuart_arbitration()
292 int ret = 0; in brcmuart_arbitration()
304 if (waits-- == 0) in brcmuart_arbitration()
320 int x; in brcmuart_init_dma_hardware()
338 daddr = priv->rx_addr; in brcmuart_init_dma_hardware()
351 daddr = priv->tx_addr; in brcmuart_init_dma_hardware()
368 struct brcmuart_priv *priv = p->port.private_data; in start_rx_dma()
369 int x; in start_rx_dma()
381 priv->rx_next_buf = 0; in start_rx_dma()
384 priv->rx_running = true; in start_rx_dma()
389 struct brcmuart_priv *priv = p->port.private_data; in stop_rx_dma()
392 if (priv->rx_running) in stop_rx_dma()
396 static int stop_tx_dma(struct uart_8250_port *p) in stop_tx_dma()
398 struct brcmuart_priv *priv = p->port.private_data; in stop_tx_dma()
405 priv->tx_running = false; in stop_tx_dma()
413 static int brcmuart_tx_dma(struct uart_8250_port *p) in brcmuart_tx_dma()
415 struct brcmuart_priv *priv = p->port.private_data; in brcmuart_tx_dma()
416 struct tty_port *tport = &p->port.state->port; in brcmuart_tx_dma()
419 if (uart_tx_stopped(&p->port) || priv->tx_running || in brcmuart_tx_dma()
420 kfifo_is_empty(&tport->xmit_fifo)) { in brcmuart_tx_dma()
424 priv->dma.tx_err = 0; in brcmuart_tx_dma()
425 tx_size = uart_fifo_out(&p->port, priv->tx_buf, UART_XMIT_SIZE); in brcmuart_tx_dma()
427 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) in brcmuart_tx_dma()
428 uart_write_wakeup(&p->port); in brcmuart_tx_dma()
434 priv->tx_running = true; in brcmuart_tx_dma()
439 static void brcmuart_rx_buf_done_isr(struct uart_port *up, int index) in brcmuart_rx_buf_done_isr()
441 struct brcmuart_priv *priv = up->private_data; in brcmuart_rx_buf_done_isr()
442 struct tty_port *tty_port = &up->state->port; in brcmuart_rx_buf_done_isr()
452 dev_err(up->dev, "RX done interrupt but DATA_RDY not found\n"); in brcmuart_rx_buf_done_isr()
459 up->icount.overrun++; in brcmuart_rx_buf_done_isr()
460 dev_warn(up->dev, "RX OVERRUN Error\n"); in brcmuart_rx_buf_done_isr()
463 up->icount.frame++; in brcmuart_rx_buf_done_isr()
464 dev_warn(up->dev, "RX FRAMING Error\n"); in brcmuart_rx_buf_done_isr()
467 up->icount.parity++; in brcmuart_rx_buf_done_isr()
468 dev_warn(up->dev, "RX PARITY Error\n"); in brcmuart_rx_buf_done_isr()
473 priv->rx_bufs + (index * RX_BUF_SIZE), in brcmuart_rx_buf_done_isr()
476 dev_warn(up->dev, "Flip buffer overrun of %d bytes\n", in brcmuart_rx_buf_done_isr()
477 length - copied); in brcmuart_rx_buf_done_isr()
478 up->icount.overrun += length - copied; in brcmuart_rx_buf_done_isr()
480 up->icount.rx += length; in brcmuart_rx_buf_done_isr()
482 priv->dma_rx_partial_buf++; in brcmuart_rx_buf_done_isr()
488 priv->rx_missing_close_timeout++; in brcmuart_rx_buf_done_isr()
490 priv->dma_rx_full_buf++; in brcmuart_rx_buf_done_isr()
497 struct brcmuart_priv *priv = up->private_data; in brcmuart_rx_isr()
498 struct device *dev = up->dev; in brcmuart_rx_isr()
504 check_isr = UDMA_INTR_RX_READY_BUF0 << priv->rx_next_buf; in brcmuart_rx_isr()
506 brcmuart_rx_buf_done_isr(up, priv->rx_next_buf); in brcmuart_rx_isr()
515 priv->rx_err++; in brcmuart_rx_isr()
517 priv->rx_timeout++; in brcmuart_rx_isr()
521 priv->rx_abort++; in brcmuart_rx_isr()
522 priv->rx_running = false; in brcmuart_rx_isr()
524 /* If not ABORT, re-enable RX buffer */ in brcmuart_rx_isr()
527 UDMA_RX_BUFx_STATUS(priv->rx_next_buf), in brcmuart_rx_isr()
530 priv->rx_next_buf++; in brcmuart_rx_isr()
531 if (priv->rx_next_buf == RX_BUFS_COUNT) in brcmuart_rx_isr()
532 priv->rx_next_buf = 0; in brcmuart_rx_isr()
538 struct brcmuart_priv *priv = up->private_data; in brcmuart_tx_isr()
539 struct device *dev = up->dev; in brcmuart_tx_isr()
541 struct tty_port *tport = &port_8250->port.state->port; in brcmuart_tx_isr()
544 if (priv->tx_running) in brcmuart_tx_isr()
548 priv->tx_running = false; in brcmuart_tx_isr()
549 if (!kfifo_is_empty(&tport->xmit_fifo) && !uart_tx_stopped(up)) in brcmuart_tx_isr()
553 static irqreturn_t brcmuart_isr(int irq, void *dev_id) in brcmuart_isr()
556 struct device *dev = up->dev; in brcmuart_isr()
557 struct brcmuart_priv *priv = up->private_data; in brcmuart_isr()
585 static int brcmuart_startup(struct uart_port *port) in brcmuart_startup() argument
587 int res; in brcmuart_startup()
588 struct uart_8250_port *up = up_to_u8250p(port); in brcmuart_startup()
589 struct brcmuart_priv *priv = up->port.private_data; in brcmuart_startup()
591 priv->shutdown = false; in brcmuart_startup()
594 * prevent serial8250_do_startup() from allocating non-existent in brcmuart_startup()
597 up->dma = NULL; in brcmuart_startup()
599 res = serial8250_do_startup(port); in brcmuart_startup()
600 if (!priv->dma_enabled) in brcmuart_startup()
608 uart_port_lock_irq(port); in brcmuart_startup()
609 up->ier &= ~UART_IER_RDI; in brcmuart_startup()
610 serial_port_out(port, UART_IER, up->ier); in brcmuart_startup()
611 uart_port_unlock_irq(port); in brcmuart_startup()
613 priv->tx_running = false; in brcmuart_startup()
614 priv->dma.rx_dma = NULL; in brcmuart_startup()
615 priv->dma.tx_dma = brcmuart_tx_dma; in brcmuart_startup()
616 up->dma = &priv->dma; in brcmuart_startup()
623 static void brcmuart_shutdown(struct uart_port *port) in brcmuart_shutdown() argument
625 struct uart_8250_port *up = up_to_u8250p(port); in brcmuart_shutdown()
626 struct brcmuart_priv *priv = up->port.private_data; in brcmuart_shutdown()
629 uart_port_lock_irqsave(port, &flags); in brcmuart_shutdown()
630 priv->shutdown = true; in brcmuart_shutdown()
631 if (priv->dma_enabled) { in brcmuart_shutdown()
643 up->dma = NULL; in brcmuart_shutdown()
645 uart_port_unlock_irqrestore(port, flags); in brcmuart_shutdown()
646 serial8250_do_shutdown(port); in brcmuart_shutdown()
655 int x; in init_real_clk_rates()
656 int rc; in init_real_clk_rates()
658 priv->default_mux_rate = clk_get_rate(priv->baud_mux_clk); in init_real_clk_rates()
659 for (x = 0; x < ARRAY_SIZE(priv->real_rates); x++) { in init_real_clk_rates()
660 if (priv->rate_table[x] == 0) { in init_real_clk_rates()
661 priv->real_rates[x] = 0; in init_real_clk_rates()
664 rc = clk_set_rate(priv->baud_mux_clk, priv->rate_table[x]); in init_real_clk_rates()
666 dev_err(dev, "Error selecting BAUD MUX clock for %u\n", in init_real_clk_rates()
667 priv->rate_table[x]); in init_real_clk_rates()
668 priv->real_rates[x] = priv->rate_table[x]; in init_real_clk_rates()
670 priv->real_rates[x] = clk_get_rate(priv->baud_mux_clk); in init_real_clk_rates()
673 clk_set_rate(priv->baud_mux_clk, priv->default_mux_rate); in init_real_clk_rates()
695 hires_err = (hires_rate - hires_baud); in find_quot()
697 hires_err = (hires_baud - hires_rate); in find_quot()
701 dev_dbg(dev, "Baud rate: %u, MUX Clk: %u, Error: %u.%u%%\n", in find_quot()
716 int rc; in set_clock_mux()
717 int i; in set_clock_mux()
718 int real_baud; in set_clock_mux()
720 /* If the Baud Mux Clock was not specified, just return */ in set_clock_mux()
721 if (priv->baud_mux_clk == NULL) in set_clock_mux()
725 quot = find_quot(up->dev, priv->default_mux_rate, baud, &percent); in set_clock_mux()
728 best_freq = priv->default_mux_rate; in set_clock_mux()
733 for (i = 0; i < ARRAY_SIZE(priv->real_rates); i++) { in set_clock_mux()
734 freq = priv->real_rates[i]; in set_clock_mux()
735 if (freq == 0 || freq == priv->default_mux_rate) in set_clock_mux()
737 quot = find_quot(up->dev, freq, baud, &percent); in set_clock_mux()
749 dev_err(up->dev, "Error, %d BAUD rate is too fast.\n", baud); in set_clock_mux()
752 rc = clk_set_rate(priv->baud_mux_clk, best_freq); in set_clock_mux()
754 dev_err(up->dev, "Error selecting BAUD MUX clock\n"); in set_clock_mux()
758 dev_err(up->dev, "Error, baud: %d has %u.%u%% error\n", in set_clock_mux()
762 dev_dbg(up->dev, "Selecting BAUD MUX rate: %u\n", best_freq); in set_clock_mux()
763 dev_dbg(up->dev, "Requested baud: %u, Actual baud: %u\n", in set_clock_mux()
769 priv->char_wait = ns_to_ktime(i); in set_clock_mux()
771 up->uartclk = best_freq; in set_clock_mux()
779 struct brcmuart_priv *priv = up->private_data; in brcmstb_set_termios()
781 if (priv->dma_enabled) in brcmstb_set_termios()
785 if (p8250->mcr & UART_MCR_AFE) in brcmstb_set_termios()
786 p8250->port.status |= UPSTAT_AUTOCTS; in brcmstb_set_termios()
787 if (priv->dma_enabled) in brcmstb_set_termios()
791 static int brcmuart_handle_irq(struct uart_port *p) in brcmuart_handle_irq()
793 unsigned int iir = serial_port_in(p, UART_IIR); in brcmuart_handle_irq()
794 struct brcmuart_priv *priv = p->private_data; in brcmuart_handle_irq()
796 unsigned int status; in brcmuart_handle_irq()
798 unsigned int ier; in brcmuart_handle_irq()
799 unsigned int mcr; in brcmuart_handle_irq()
800 int handled = 0; in brcmuart_handle_irq()
806 if (((iir & UART_IIR_ID) == UART_IIR_RX_TIMEOUT) && !(priv->shutdown)) { in brcmuart_handle_irq()
818 if ((ier & UART_IER_RDI) && (up->mcr & UART_MCR_AFE)) { in brcmuart_handle_irq()
824 hrtimer_start(&priv->hrt, priv->char_wait, in brcmuart_handle_irq()
842 struct uart_port *p = priv->up; in brcmuart_hrtimer_func()
844 unsigned int status; in brcmuart_hrtimer_func()
847 if (priv->shutdown) in brcmuart_hrtimer_func()
859 priv->rx_bad_timeout_no_char++; in brcmuart_hrtimer_func()
861 priv->rx_bad_timeout_late_char++; in brcmuart_hrtimer_func()
864 /* re-enable receive unless upper layer has disabled it */ in brcmuart_hrtimer_func()
865 if ((up->ier & (UART_IER_RLSI | UART_IER_RDI)) == in brcmuart_hrtimer_func()
880 .compatible = "brcm,bcm7278-uart",
884 .compatible = "brcm,bcm7271-uart",
894 if (priv->rx_bufs) in brcmuart_free_bufs()
895 dma_free_coherent(dev, priv->rx_size, priv->rx_bufs, in brcmuart_free_bufs()
896 priv->rx_addr); in brcmuart_free_bufs()
897 if (priv->tx_buf) in brcmuart_free_bufs()
898 dma_free_coherent(dev, priv->tx_size, priv->tx_buf, in brcmuart_free_bufs()
899 priv->tx_addr); in brcmuart_free_bufs()
902 static void brcmuart_throttle(struct uart_port *port) in brcmuart_throttle() argument
904 struct brcmuart_priv *priv = port->private_data; in brcmuart_throttle()
909 static void brcmuart_unthrottle(struct uart_port *port) in brcmuart_unthrottle() argument
911 struct brcmuart_priv *priv = port->private_data; in brcmuart_unthrottle()
917 static int debugfs_stats_show(struct seq_file *s, void *unused) in debugfs_stats_show()
919 struct brcmuart_priv *priv = s->private; in debugfs_stats_show()
922 priv->rx_err); in debugfs_stats_show()
924 priv->rx_timeout); in debugfs_stats_show()
926 priv->rx_abort); in debugfs_stats_show()
928 priv->rx_bad_timeout_late_char); in debugfs_stats_show()
930 priv->rx_bad_timeout_no_char); in debugfs_stats_show()
932 priv->rx_missing_close_timeout); in debugfs_stats_show()
933 if (priv->dma_enabled) { in debugfs_stats_show()
935 priv->dma_rx_partial_buf); in debugfs_stats_show()
937 priv->dma_rx_full_buf); in debugfs_stats_show()
946 priv->debugfs_dir = debugfs_create_dir(device, brcmuart_debugfs_root); in brcmuart_init_debugfs()
947 debugfs_create_file("stats", 0444, priv->debugfs_dir, priv, in brcmuart_init_debugfs()
952 static int brcmuart_probe(struct platform_device *pdev) in brcmuart_probe()
957 struct device *dev = &pdev->dev; in brcmuart_probe()
963 int ret; in brcmuart_probe()
964 int x; in brcmuart_probe()
965 int dma_irq; in brcmuart_probe()
973 return -ENOMEM; in brcmuart_probe()
975 of_id = of_match_node(brcmuart_dt_ids, dev->of_node); in brcmuart_probe()
976 if (!of_id || !of_id->data) in brcmuart_probe()
977 priv->rate_table = brcmstb_rate_table; in brcmuart_probe()
979 priv->rate_table = of_id->data; in brcmuart_probe()
986 priv->regs[x] = devm_ioremap(dev, regs->start, in brcmuart_probe()
988 if (!priv->regs[x]) in brcmuart_probe()
989 return -ENOMEM; in brcmuart_probe()
991 mapbase = regs->start; in brcmuart_probe()
992 membase = priv->regs[x]; in brcmuart_probe()
998 return dev_err_probe(dev, -EINVAL, "%s registers not specified\n", in brcmuart_probe()
1013 priv->dma_enabled = true; in brcmuart_probe()
1025 dev_dbg(dev, "DMA is %senabled\n", priv->dma_enabled ? "" : "not "); in brcmuart_probe()
1028 up.port.type = PORT_BCM7271; in brcmuart_probe()
1029 up.port.dev = dev; in brcmuart_probe()
1030 up.port.mapbase = mapbase; in brcmuart_probe()
1031 up.port.membase = membase; in brcmuart_probe()
1032 up.port.handle_irq = brcmuart_handle_irq; in brcmuart_probe()
1033 up.port.flags = UPF_BOOT_AUTOCONF | UPF_FIXED_PORT | UPF_FIXED_TYPE; in brcmuart_probe()
1034 up.port.private_data = priv; in brcmuart_probe()
1036 ret = uart_read_port_properties(&up.port); in brcmuart_probe()
1040 up.port.regshift = 2; in brcmuart_probe()
1041 up.port.iotype = device_is_big_endian(dev) ? UPIO_MEM32BE : UPIO_MEM32; in brcmuart_probe()
1049 dev_dbg(dev, "BAUD MUX clock found\n"); in brcmuart_probe()
1051 priv->baud_mux_clk = baud_mux_clk; in brcmuart_probe()
1053 up.port.uartclk = priv->default_mux_rate; in brcmuart_probe()
1055 dev_dbg(dev, "BAUD MUX clock not specified\n"); in brcmuart_probe()
1059 hrtimer_init(&priv->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); in brcmuart_probe()
1060 priv->hrt.function = brcmuart_hrtimer_func; in brcmuart_probe()
1062 up.port.shutdown = brcmuart_shutdown; in brcmuart_probe()
1063 up.port.startup = brcmuart_startup; in brcmuart_probe()
1064 up.port.throttle = brcmuart_throttle; in brcmuart_probe()
1065 up.port.unthrottle = brcmuart_unthrottle; in brcmuart_probe()
1066 up.port.set_termios = brcmstb_set_termios; in brcmuart_probe()
1068 if (priv->dma_enabled) { in brcmuart_probe()
1069 priv->rx_size = RX_BUF_SIZE * RX_BUFS_COUNT; in brcmuart_probe()
1070 priv->rx_bufs = dma_alloc_coherent(dev, in brcmuart_probe()
1071 priv->rx_size, in brcmuart_probe()
1072 &priv->rx_addr, GFP_KERNEL); in brcmuart_probe()
1073 if (!priv->rx_bufs) { in brcmuart_probe()
1074 ret = -ENOMEM; in brcmuart_probe()
1077 priv->tx_size = UART_XMIT_SIZE; in brcmuart_probe()
1078 priv->tx_buf = dma_alloc_coherent(dev, in brcmuart_probe()
1079 priv->tx_size, in brcmuart_probe()
1080 &priv->tx_addr, GFP_KERNEL); in brcmuart_probe()
1081 if (!priv->tx_buf) { in brcmuart_probe()
1082 ret = -ENOMEM; in brcmuart_probe()
1089 dev_err_probe(dev, ret, "unable to register 8250 port\n"); in brcmuart_probe()
1092 priv->line = ret; in brcmuart_probe()
1094 priv->up = &new_port->port; in brcmuart_probe()
1095 if (priv->dma_enabled) { in brcmuart_probe()
1102 IRQF_SHARED, "uart DMA irq", &new_port->port); in brcmuart_probe()
1109 brcmuart_init_debugfs(priv, dev_name(&pdev->dev)); in brcmuart_probe()
1113 serial8250_unregister_port(priv->line); in brcmuart_probe()
1117 if (priv->dma_enabled) in brcmuart_probe()
1126 debugfs_remove_recursive(priv->debugfs_dir); in brcmuart_remove()
1127 hrtimer_cancel(&priv->hrt); in brcmuart_remove()
1128 serial8250_unregister_port(priv->line); in brcmuart_remove()
1129 brcmuart_free_bufs(&pdev->dev, priv); in brcmuart_remove()
1130 if (priv->dma_enabled) in brcmuart_remove()
1134 static int __maybe_unused brcmuart_suspend(struct device *dev) in brcmuart_suspend()
1137 struct uart_8250_port *up = serial8250_get_port(priv->line); in brcmuart_suspend()
1138 struct uart_port *port = &up->port; in brcmuart_suspend() local
1145 uart_port_lock_irqsave(port, &flags); in brcmuart_suspend()
1146 priv->saved_mctrl = port->mctrl; in brcmuart_suspend()
1147 port->mctrl &= ~TIOCM_RTS; in brcmuart_suspend()
1148 uart_port_unlock_irqrestore(port, flags); in brcmuart_suspend()
1150 serial8250_suspend_port(priv->line); in brcmuart_suspend()
1151 clk_disable_unprepare(priv->baud_mux_clk); in brcmuart_suspend()
1156 static int __maybe_unused brcmuart_resume(struct device *dev) in brcmuart_resume()
1159 struct uart_8250_port *up = serial8250_get_port(priv->line); in brcmuart_resume()
1160 struct uart_port *port = &up->port; in brcmuart_resume() local
1162 int ret; in brcmuart_resume()
1164 ret = clk_prepare_enable(priv->baud_mux_clk); in brcmuart_resume()
1166 dev_err(dev, "Error enabling BAUD MUX clock\n"); in brcmuart_resume()
1172 ret = clk_set_rate(priv->baud_mux_clk, priv->default_mux_rate); in brcmuart_resume()
1174 dev_err(dev, "Error restoring default BAUD MUX clock\n"); in brcmuart_resume()
1175 if (priv->dma_enabled) { in brcmuart_resume()
1178 return(-EBUSY); in brcmuart_resume()
1181 start_rx_dma(serial8250_get_port(priv->line)); in brcmuart_resume()
1183 serial8250_resume_port(priv->line); in brcmuart_resume()
1185 if (priv->saved_mctrl & TIOCM_RTS) { in brcmuart_resume()
1187 uart_port_lock_irqsave(port, &flags); in brcmuart_resume()
1188 port->mctrl |= TIOCM_RTS; in brcmuart_resume()
1189 port->ops->set_mctrl(port, port->mctrl); in brcmuart_resume()
1190 uart_port_unlock_irqrestore(port, flags); in brcmuart_resume()
1202 .name = "bcm7271-uart",
1210 static int __init brcmuart_init(void) in brcmuart_init()
1212 int ret; in brcmuart_init()
1234 MODULE_DESCRIPTION("Broadcom NS16550A compatible serial port driver");