mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Pierre-Olivier Huard <pierre-olivier.huard@rtone.fr>
To: barebox@lists.infradead.org
Cc: sebastien.bourdelin@rtone.fr, a.fatoum@pengutronix.de,
	oss-contrib@rtone.fr,
	Pierre-Olivier Huard <pierre-olivier.huard@rtone.fr>
Subject: [PATCH 2/2] serial: ns16550: add support for half duplex rs485
Date: Tue,  6 Feb 2024 16:22:39 +0100	[thread overview]
Message-ID: <20240206152239.341460-3-pierre-olivier.huard@rtone.fr> (raw)
In-Reply-To: <20240206152239.341460-1-pierre-olivier.huard@rtone.fr>

Some RS485 serial interfaces does not use correctly RTS as Receiver
Enable and Data Enable, the internal receiver of the ns16550 must be
disabled during transmit to avoid loopback.

By default the option is enabled (when the property
'linux,rs485-enabled-at-boot-time' is present), and can be disabled with
the property 'rs485-rx-during-tx'.

Signed-off-by: Pierre-Olivier Huard <pierre-olivier.huard@rtone.fr>
---
 drivers/serial/serial_ns16550.c | 9 +++++++++
 drivers/serial/serial_ns16550.h | 4 ++++
 2 files changed, 13 insertions(+)

diff --git a/drivers/serial/serial_ns16550.c b/drivers/serial/serial_ns16550.c
index 73d8365dac..1b1692658f 100644
--- a/drivers/serial/serial_ns16550.c
+++ b/drivers/serial/serial_ns16550.c
@@ -43,6 +43,7 @@ struct ns16550_priv {
 
 	bool rs485_mode;
 	bool rs485_rts_active_low;
+	bool rs485_rx_during_tx;
 };
 
 struct ns16550_drvdata {
@@ -279,6 +280,9 @@ static void ns16550_putc(struct console_device *cdev, char c)
 			ns16550_write(cdev, MCR_RTS, mcr);
 		else
 			ns16550_write(cdev, 0, mcr);
+
+		if (!priv->rs485_rx_during_tx)
+			ns16550_write(cdev, CNTL_TXEN, cntl);
 	}
 
 	ns16550_write(cdev, c, thr);
@@ -292,6 +296,9 @@ static void ns16550_putc(struct console_device *cdev, char c)
 			ns16550_write(cdev, 0, mcr);
 		else
 			ns16550_write(cdev, MCR_RTS, mcr);
+
+		if (!priv->rs485_rx_during_tx)
+			ns16550_write(cdev, CNTL_TXEN | CNTL_RXEN, cntl);
 	}
 }
 
@@ -350,6 +357,8 @@ static void ns16550_probe_dt(struct device *dev, struct ns16550_priv *priv)
 		of_property_read_bool(np, "rs485-rts-active-low");
 	priv->rs485_mode =
 		of_property_read_bool(np, "linux,rs485-enabled-at-boot-time");
+	priv->rs485_rx_during_tx =
+		of_property_read_bool(np, "rs485-rx-during-tx");
 
 	switch (width) {
 	case 1:
diff --git a/drivers/serial/serial_ns16550.h b/drivers/serial/serial_ns16550.h
index 1c5d9d551a..2d941cb8d1 100644
--- a/drivers/serial/serial_ns16550.h
+++ b/drivers/serial/serial_ns16550.h
@@ -36,6 +36,7 @@
 #define lsr		5
 #define msr		6
 #define scr		7
+#define cntl	8
 
 #define thr		rbr
 #define iir		fcr
@@ -76,6 +77,9 @@
 /* Transmitter FIFO completely empty */
 #define LSR_EMPTY	(LSR_THRE | LSR_TEMT)
 
+#define CNTL_RXEN	0x01
+#define CNTL_TXEN	0x02
+
 /* useful defaults for LCR */
 #define LCR_8N1		0x03
 
-- 
2.43.0




  parent reply	other threads:[~2024-02-06 15:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-06 15:22 [PATCH 0/2] serial: ns16550: support " Pierre-Olivier Huard
2024-02-06 15:22 ` [PATCH 1/2] serial: ns16550: add basic support for rs485 Pierre-Olivier Huard
2024-02-06 15:22 ` Pierre-Olivier Huard [this message]
2024-02-08  7:15 ` [PATCH 0/2] serial: ns16550: support half duplex rs485 Sascha Hauer

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=20240206152239.341460-3-pierre-olivier.huard@rtone.fr \
    --to=pierre-olivier.huard@rtone.fr \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=oss-contrib@rtone.fr \
    --cc=sebastien.bourdelin@rtone.fr \
    /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