serial: uartps: Add a timeout to the tx empty wait
authorShubhrajyoti Datta <shubhrajyoti.datta@xilinx.com>
Mon, 10 Feb 2020 17:18:25 +0000 (10:18 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 14 Feb 2020 21:32:22 +0000 (16:32 -0500)
[ Upstream commit 277375b864e8147975b064b513f491e2a910e66a ]

In case the cable is not connected then the target gets into
an infinite wait for tx empty.
Add a timeout to the tx empty wait.

Reported-by: Jean-Francois Dagenais <jeff.dagenais@gmail.com>
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@xilinx.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/tty/serial/xilinx_uartps.c

index 0dbfd02e3b196b0317c150739e138b92e9a8bd96..9826a15bfd8717425de45eabdd30dede67da3970 100644 (file)
@@ -31,6 +31,7 @@
 #include <linux/of.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
+#include <linux/iopoll.h>
 
 #define CDNS_UART_TTY_NAME     "ttyPS"
 #define CDNS_UART_NAME         "xuartps"
@@ -39,6 +40,7 @@
 #define CDNS_UART_NR_PORTS     2
 #define CDNS_UART_FIFO_SIZE    64      /* FIFO size */
 #define CDNS_UART_REGISTER_SPACE       0x1000
+#define TX_TIMEOUT             500000
 
 /* Rx Trigger level */
 static int rx_trigger_level = 56;
@@ -685,16 +687,20 @@ static void cdns_uart_set_termios(struct uart_port *port,
        unsigned int cval = 0;
        unsigned int baud, minbaud, maxbaud;
        unsigned long flags;
-       unsigned int ctrl_reg, mode_reg;
+       unsigned int ctrl_reg, mode_reg, val;
+       int err;
 
        spin_lock_irqsave(&port->lock, flags);
 
        /* Wait for the transmit FIFO to empty before making changes */
        if (!(readl(port->membase + CDNS_UART_CR) &
                                CDNS_UART_CR_TX_DIS)) {
-               while (!(readl(port->membase + CDNS_UART_SR) &
-                               CDNS_UART_SR_TXEMPTY)) {
-                       cpu_relax();
+               err = readl_poll_timeout(port->membase + CDNS_UART_SR,
+                                        val, (val & CDNS_UART_SR_TXEMPTY),
+                                        1000, TX_TIMEOUT);
+               if (err) {
+                       dev_err(port->dev, "timed out waiting for tx empty");
+                       return;
                }
        }