Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
From: Larisa Grigore <larisa.grigore@oss.nxp.com>
To: gregkh@linuxfoundation.org, jirislaby@kernel.org,
	robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
	sumit.semwal@linaro.org, christian.koenig@amd.com,
	chester62515@gmail.com, cosmin.stoica@nxp.com,
	adrian.nitu@freescale.com, stefan-gabriel.mirea@nxp.com,
	Mihaela.Martinas@freescale.com
Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org,
	devicetree@vger.kernel.org, linux-media@vger.kernel.org,
	dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
	s32@nxp.com, imx@lists.linux.dev, clizzi@redhat.com,
	aruizrui@redhat.com, eballetb@redhat.com, echanude@redhat.com,
	jkangas@redhat.com, Larisa Grigore <larisa.grigore@oss.nxp.com>
Subject: [PATCH 06/13] serial: linflexuart: Ensure FIFO is empty when entering INITM
Date: Mon, 16 Feb 2026 16:01:58 +0100	[thread overview]
Message-ID: <20260216150205.212318-7-larisa.grigore@oss.nxp.com> (raw)
In-Reply-To: <20260216150205.212318-1-larisa.grigore@oss.nxp.com>

In FIFO mode, wait until UARTCR.TDFL_TFC(number Tx FIFO) entries reach 0
before entering INITM mode.
Failing to do so may lead to undefined behavior, such as:
- corrupted characters being printed.
- the device is not able to receive or transmit any character.

In linflex_set_termios, transmission and reception should be disabled
before entering INITM mode, as already done in linflex_setup_watermark.

This patch corrects the behavior that was previously addressed by the
earlycon workaround, making that workaround no longer necessary. The
next patch will remove it.

Fixes: 09864c1cdf5c ("tty: serial: Add linflexuart driver for S32V234")
Signed-off-by: Larisa Grigore <larisa.grigore@oss.nxp.com>
---
 drivers/tty/serial/fsl_linflexuart.c | 45 ++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/serial/fsl_linflexuart.c b/drivers/tty/serial/fsl_linflexuart.c
index 768b3c67a614..c1d069dc8089 100644
--- a/drivers/tty/serial/fsl_linflexuart.c
+++ b/drivers/tty/serial/fsl_linflexuart.c
@@ -3,7 +3,7 @@
  * Freescale LINFlexD UART serial port driver
  *
  * Copyright 2012-2016 Freescale Semiconductor, Inc.
- * Copyright 2017-2019 NXP
+ * Copyright 2017-2019, 2021 NXP
  */
 
 #include <linux/console.h>
@@ -74,6 +74,17 @@
 
 #define LINFLEXD_UARTCR_ROSE		BIT(23)
 
+#define LINFLEXD_UARTCR_RDFLRFC_OFFSET	10
+#define LINFLEXD_UARTCR_RDFLRFC_MASK	(0x7 << LINFLEXD_UARTCR_RDFLRFC_OFFSET)
+#define LINFLEXD_UARTCR_RDFLRFC(uartcr)	(((uartcr) \
+					& LINFLEXD_UARTCR_RDFLRFC_MASK) >> \
+					LINFLEXD_UARTCR_RDFLRFC_OFFSET)
+#define LINFLEXD_UARTCR_TDFLTFC_OFFSET	13
+#define LINFLEXD_UARTCR_TDFLTFC_MASK	(0x7 << LINFLEXD_UARTCR_TDFLTFC_OFFSET)
+#define LINFLEXD_UARTCR_TDFLTFC(uartcr)	(((uartcr) \
+					& LINFLEXD_UARTCR_TDFLTFC_MASK) >> \
+					LINFLEXD_UARTCR_TDFLTFC_OFFSET)
+
 #define LINFLEXD_UARTCR_RFBM		BIT(9)
 #define LINFLEXD_UARTCR_TFBM		BIT(8)
 #define LINFLEXD_UARTCR_WL1		BIT(7)
@@ -140,6 +151,17 @@ static struct {
 } earlycon_buf;
 #endif
 
+static inline void linflex_wait_tx_fifo_empty(struct uart_port *port)
+{
+	unsigned long cr = readl(port->membase + UARTCR);
+
+	if (!(cr & LINFLEXD_UARTCR_TFBM))
+		return;
+
+	while (LINFLEXD_UARTCR_TDFLTFC(readl(port->membase + UARTCR)))
+		;
+}
+
 static void linflex_stop_tx(struct uart_port *port)
 {
 	unsigned long ier;
@@ -326,6 +348,11 @@ static void linflex_setup_watermark(struct uart_port *sport)
 	cr &= ~(LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN);
 	writel(cr, sport->membase + UARTCR);
 
+	/* In FIFO mode, we should make sure the fifo is empty
+	 * before entering INITM.
+	 */
+	linflex_wait_tx_fifo_empty(sport);
+
 	/* Enter initialization mode by setting INIT bit */
 
 	/* set the Linflex in master mode and activate by-pass filter */
@@ -412,8 +439,17 @@ linflex_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	uart_port_lock_irqsave(port, &flags);
 
-	cr = readl(port->membase + UARTCR);
-	old_cr = cr;
+	old_cr = readl(port->membase + UARTCR) &
+		~(LINFLEXD_UARTCR_RXEN | LINFLEXD_UARTCR_TXEN);
+	cr = old_cr;
+
+	/* In FIFO mode, we should make sure the fifo is empty
+	 * before entering INITM.
+	 */
+	linflex_wait_tx_fifo_empty(port);
+
+	/* disable transmit and receive */
+	writel(old_cr, port->membase + UARTCR);
 
 	/* Enter initialization mode by setting INIT bit */
 	cr1 = LINFLEXD_LINCR1_INIT | LINFLEXD_LINCR1_MME;
@@ -510,6 +546,9 @@ linflex_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	writel(cr1, port->membase + LINCR1);
 
+	cr |= (LINFLEXD_UARTCR_TXEN) | (LINFLEXD_UARTCR_RXEN);
+	writel(cr, port->membase + UARTCR);
+
 	uart_port_unlock_irqrestore(port, flags);
 }
 
-- 
2.47.0


  parent reply	other threads:[~2026-02-16 15:03 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-16 15:01 [PATCH 00/13] Add DMA support for LINFlexD UART driver Larisa Grigore
2026-02-16 15:01 ` [PATCH 01/13] serial: linflexuart: Fix locking in set_termios Larisa Grigore
2026-02-16 20:16   ` Frank Li
2026-02-18 11:58     ` Larisa Ileana Grigore
2026-02-16 15:01 ` [PATCH 02/13] serial: linflexuart: Clean SLEEP bit in LINCR1 after suspend Larisa Grigore
2026-02-16 20:22   ` Frank Li
2026-02-18 12:09     ` Larisa Ileana Grigore
2026-02-16 15:01 ` [PATCH 03/13] serial: linflexuart: Check FIFO full before writing Larisa Grigore
2026-02-16 15:01 ` [PATCH 04/13] serial: linflexuart: Correctly clear UARTSR in buffer mode Larisa Grigore
2026-02-16 15:01 ` [PATCH 05/13] serial: linflexuart: Update RXEN/TXEN outside INITM mode Larisa Grigore
2026-02-16 15:01 ` Larisa Grigore [this message]
2026-02-16 15:01 ` [PATCH 07/13] serial: linflexuart: Revert earlycon workaround Larisa Grigore
2026-02-16 15:02 ` [PATCH 08/13] dt-bindings: serial: fsl-linflexuart: add clock input properties Larisa Grigore
2026-02-16 15:10   ` Krzysztof Kozlowski
2026-02-18 13:26     ` Larisa Ileana Grigore
2026-02-18 13:29       ` Krzysztof Kozlowski
2026-02-18 13:57         ` Larisa Ileana Grigore
2026-02-18 19:48           ` Krzysztof Kozlowski
2026-02-16 15:02 ` [PATCH 09/13] dt-bindings: serial: fsl-linflexuart: add dma properties Larisa Grigore
2026-02-16 15:10   ` Krzysztof Kozlowski
2026-02-18 14:44     ` Larisa Ileana Grigore
2026-02-18 19:49       ` Krzysztof Kozlowski
2026-02-16 15:29   ` Daniel Baluta
2026-02-17  8:10     ` Krzysztof Kozlowski
2026-02-17  8:39       ` Daniel Baluta
2026-02-16 15:02 ` [PATCH 10/13] serial: linflexuart: Add support for changing baudrate Larisa Grigore
2026-02-16 15:02 ` [PATCH 11/13] serial: linflexuart: Add support for configurable stop bits Larisa Grigore
2026-02-16 15:02 ` [PATCH 12/13] serial: linflexuart: Add DMA support Larisa Grigore
2026-02-16 15:11   ` Krzysztof Kozlowski
2026-02-16 20:48   ` kernel test robot
2026-02-17  3:26   ` kernel test robot
2026-02-19  8:22   ` Dan Carpenter
2026-02-16 15:02 ` [PATCH 13/13] serial: linflexuart: Avoid stopping DMA during receive operations Larisa Grigore
2026-02-27 14:03 ` [PATCH 00/13] Add DMA support for LINFlexD UART driver Jared Kangas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260216150205.212318-7-larisa.grigore@oss.nxp.com \
    --to=larisa.grigore@oss.nxp.com \
    --cc=Mihaela.Martinas@freescale.com \
    --cc=adrian.nitu@freescale.com \
    --cc=aruizrui@redhat.com \
    --cc=chester62515@gmail.com \
    --cc=christian.koenig@amd.com \
    --cc=clizzi@redhat.com \
    --cc=conor+dt@kernel.org \
    --cc=cosmin.stoica@nxp.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=eballetb@redhat.com \
    --cc=echanude@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=imx@lists.linux.dev \
    --cc=jirislaby@kernel.org \
    --cc=jkangas@redhat.com \
    --cc=krzk+dt@kernel.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=s32@nxp.com \
    --cc=stefan-gabriel.mirea@nxp.com \
    --cc=sumit.semwal@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox