[PATCH 2/2] serial: ns16550: add support for half duplex rs485

Pierre-Olivier Huard pierre-olivier.huard at rtone.fr
Tue Feb 6 07:22:39 PST 2024


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 at 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




More information about the barebox mailing list