tty: serial: fsl_lpuart: avoid idle preamble pending if CTS is enabled

Message ID 20240304020712.238637-1-sherry.sun@nxp.com
State New
Headers
Series tty: serial: fsl_lpuart: avoid idle preamble pending if CTS is enabled |

Commit Message

Sherry Sun March 4, 2024, 2:07 a.m. UTC
  If the remote uart device is not connected or not enabled after booting
up, the CTS line is high by default. At this time, if we enable the flow
control when opening the device(for example, using “stty -F /dev/ttyLP4
crtscts” command), there will be a pending idle preamble(first writing 0
and then writing 1 to UARTCTRL_TE will queue an idle preamble) that
cannot be sent out, resulting in the uart port fail to close(waiting for
TX empty), so the user space stty will have to wait for a long time or
forever.

This is an LPUART IP bug(idle preamble has higher priority than CTS),
here add a workaround patch to enable TX CTS after enabling UARTCTRL_TE,
so that the idle preamble does not get stuck due to CTS is deasserted.

Signed-off-by: Sherry Sun <sherry.sun@nxp.com>
---
 drivers/tty/serial/fsl_lpuart.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
  

Comments

Sverdlin, Alexander March 4, 2024, 7:13 a.m. UTC | #1
Hi Sherry,

thank you for the fix!

On Mon, 2024-03-04 at 10:07 +0800, Sherry Sun wrote:
> If the remote uart device is not connected or not enabled after booting
> up, the CTS line is high by default. At this time, if we enable the flow
> control when opening the device(for example, using “stty -F /dev/ttyLP4
> crtscts” command), there will be a pending idle preamble(first writing 0
> and then writing 1 to UARTCTRL_TE will queue an idle preamble) that
> cannot be sent out, resulting in the uart port fail to close(waiting for
> TX empty), so the user space stty will have to wait for a long time or
> forever.
> 
> This is an LPUART IP bug(idle preamble has higher priority than CTS),
> here add a workaround patch to enable TX CTS after enabling UARTCTRL_TE,
> so that the idle preamble does not get stuck due to CTS is deasserted.
> 
> Signed-off-by: Sherry Sun <sherry.sun@nxp.com>
> ---
>  drivers/tty/serial/fsl_lpuart.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
> index 5ddf110aedbe..dce1b5851de0 100644
> --- a/drivers/tty/serial/fsl_lpuart.c
> +++ b/drivers/tty/serial/fsl_lpuart.c
> @@ -2345,8 +2345,12 @@ lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
>  
>         lpuart32_write(&sport->port, bd, UARTBAUD);
>         lpuart32_serial_setbrg(sport, baud);
> -       lpuart32_write(&sport->port, modem, UARTMODIR);
> +       /* disable CTS before enabling UARTCTRL_TE to avoid pending idle preamble */
> +       lpuart32_write(&sport->port, modem & ~UARTMODIR_TXCTSE, UARTMODIR);
>         lpuart32_write(&sport->port, ctrl, UARTCTRL);
> +       /* re-enable the CTS if needed */
> +       lpuart32_write(&sport->port, modem, UARTMODIR);
> +
>         /* restore control register */

The fix makes sense to me!
I see only one issue with above comment, which commit 380c966c093e7
already have put quite sub-optimal (after the actual write), but
your commit now shifts it even further making it completely dangling.
Should it be before last UARTCTRL write? 

>  
>         if ((ctrl & (UARTCTRL_PE | UARTCTRL_M)) == UARTCTRL_PE)

-- 
Alexander Sverdlin
Siemens AG
www.siemens.com
  
Sherry Sun March 4, 2024, 7:31 a.m. UTC | #2
> -----Original Message-----
> From: Sverdlin, Alexander <alexander.sverdlin@siemens.com>
> Sent: Monday, March 4, 2024 3:14 PM
> To: u.kleine-koenig@pengutronix.de; Sherry Sun <sherry.sun@nxp.com>;
> gregkh@linuxfoundation.org; ilpo.jarvinen@linux.intel.com;
> robh@kernel.org; Shenwei Wang <shenwei.wang@nxp.com>;
> tglx@linutronix.de; jirislaby@kernel.org; robert.hodaszi@digi.com
> Cc: linux-serial@vger.kernel.org; imx@lists.linux.dev; linux-
> kernel@vger.kernel.org; Frank Li <frank.li@nxp.com>
> Subject: Re: [PATCH] tty: serial: fsl_lpuart: avoid idle preamble pending if CTS
> is enabled
> 
> Hi Sherry,
> 
> thank you for the fix!
> 
> On Mon, 2024-03-04 at 10:07 +0800, Sherry Sun wrote:
> > If the remote uart device is not connected or not enabled after
> > booting up, the CTS line is high by default. At this time, if we
> > enable the flow control when opening the device(for example, using
> > "stty -F /dev/ttyLP4 crtscts" command), there will be a pending idle
> > preamble(first writing 0 and then writing 1 to UARTCTRL_TE will queue
> > an idle preamble) that cannot be sent out, resulting in the uart port
> > fail to close(waiting for TX empty), so the user space stty will have
> > to wait for a long time or forever.
> >
> > This is an LPUART IP bug(idle preamble has higher priority than CTS),
> > here add a workaround patch to enable TX CTS after enabling
> > UARTCTRL_TE, so that the idle preamble does not get stuck due to CTS is
> deasserted.
> >
> > Signed-off-by: Sherry Sun <sherry.sun@nxp.com>
> > ---
> >  drivers/tty/serial/fsl_lpuart.c | 6 +++++-
> >  1 file changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/tty/serial/fsl_lpuart.c
> > b/drivers/tty/serial/fsl_lpuart.c index 5ddf110aedbe..dce1b5851de0
> > 100644
> > --- a/drivers/tty/serial/fsl_lpuart.c
> > +++ b/drivers/tty/serial/fsl_lpuart.c
> > @@ -2345,8 +2345,12 @@ lpuart32_set_termios(struct uart_port *port,
> > struct ktermios *termios,
> >
> >         lpuart32_write(&sport->port, bd, UARTBAUD);
> >         lpuart32_serial_setbrg(sport, baud);
> > -       lpuart32_write(&sport->port, modem, UARTMODIR);
> > +       /* disable CTS before enabling UARTCTRL_TE to avoid pending
> > +idle preamble */
> > +       lpuart32_write(&sport->port, modem & ~UARTMODIR_TXCTSE,
> > +UARTMODIR);
> >         lpuart32_write(&sport->port, ctrl, UARTCTRL);
> > +       /* re-enable the CTS if needed */
> > +       lpuart32_write(&sport->port, modem, UARTMODIR);
> > +
> >         /* restore control register */
> 
> The fix makes sense to me!
> I see only one issue with above comment, which commit 380c966c093e7
> already have put quite sub-optimal (after the actual write), but your commit
> now shifts it even further making it completely dangling.
> Should it be before last UARTCTRL write?

Hi Alexander, good catch, I will move the "/* restore control register */" message to the appropriate place in V2. Thanks!

Best Regards
Sherry
  
Fabio Estevam March 4, 2024, 11:09 a.m. UTC | #3
Hi Sherry,

On Mon, Mar 4, 2024 at 4:32 AM Sherry Sun <sherry.sun@nxp.com> wrote:

> Hi Alexander, good catch, I will move the "/* restore control register */" message to the appropriate place in V2. Thanks!

Please add a Fixes tag in v2.
  
Sherry Sun March 4, 2024, 11:11 a.m. UTC | #4
> -----Original Message-----
> From: Fabio Estevam <festevam@gmail.com>
> Sent: Monday, March 4, 2024 7:10 PM
> To: Sherry Sun <sherry.sun@nxp.com>
> Cc: Sverdlin, Alexander <alexander.sverdlin@siemens.com>; u.kleine-
> koenig@pengutronix.de; gregkh@linuxfoundation.org;
> ilpo.jarvinen@linux.intel.com; robh@kernel.org; Shenwei Wang
> <shenwei.wang@nxp.com>; tglx@linutronix.de; jirislaby@kernel.org;
> robert.hodaszi@digi.com; linux-serial@vger.kernel.org; imx@lists.linux.dev;
> linux-kernel@vger.kernel.org; Frank Li <frank.li@nxp.com>
> Subject: Re: [PATCH] tty: serial: fsl_lpuart: avoid idle preamble pending if CTS
> is enabled
> 
> Hi Sherry,
> 
> On Mon, Mar 4, 2024 at 4:32 AM Sherry Sun <sherry.sun@nxp.com> wrote:
> 
> > Hi Alexander, good catch, I will move the "/* restore control register */"
> message to the appropriate place in V2. Thanks!
> 
> Please add a Fixes tag in v2.

Sure, will add, thanks!

Best Regards
Sherry
  

Patch

diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
index 5ddf110aedbe..dce1b5851de0 100644
--- a/drivers/tty/serial/fsl_lpuart.c
+++ b/drivers/tty/serial/fsl_lpuart.c
@@ -2345,8 +2345,12 @@  lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	lpuart32_write(&sport->port, bd, UARTBAUD);
 	lpuart32_serial_setbrg(sport, baud);
-	lpuart32_write(&sport->port, modem, UARTMODIR);
+	/* disable CTS before enabling UARTCTRL_TE to avoid pending idle preamble */
+	lpuart32_write(&sport->port, modem & ~UARTMODIR_TXCTSE, UARTMODIR);
 	lpuart32_write(&sport->port, ctrl, UARTCTRL);
+	/* re-enable the CTS if needed */
+	lpuart32_write(&sport->port, modem, UARTMODIR);
+
 	/* restore control register */
 
 	if ((ctrl & (UARTCTRL_PE | UARTCTRL_M)) == UARTCTRL_PE)