mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/2] serial: ns16550: support half duplex rs485
@ 2024-02-06 15:22 Pierre-Olivier Huard
  2024-02-06 15:22 ` [PATCH 1/2] serial: ns16550: add basic support for rs485 Pierre-Olivier Huard
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Pierre-Olivier Huard @ 2024-02-06 15:22 UTC (permalink / raw)
  To: barebox; +Cc: sebastien.bourdelin, a.fatoum, oss-contrib, Pierre-Olivier Huard

The default ns16550 driver does not support rs485. This patchset adds
support for half duplex rs485 to the ns16550 driver. Basic handling of the RTS
signal is done to enable the rs485 transceivers.

Some properties are now parsed from the device tree:
- 'linux,rs485-enabled-at-boot-time', enabling RTS toggling when sending data
- 'rs485-rts-active-low', inverting the RTS signal
- 'rs485-rx-during-tx', enabling RX during TX, disabled by default

Pierre-Olivier Huard (2):
  serial: ns16550: add basic support for rs485
  serial: ns16550: add support for half duplex rs485

 drivers/serial/serial_ns16550.c | 42 +++++++++++++++++++++++++++++++--
 drivers/serial/serial_ns16550.h |  7 ++++++
 2 files changed, 47 insertions(+), 2 deletions(-)

-- 
2.43.0




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] serial: ns16550: add basic support for rs485
  2024-02-06 15:22 [PATCH 0/2] serial: ns16550: support half duplex rs485 Pierre-Olivier Huard
@ 2024-02-06 15:22 ` Pierre-Olivier Huard
  2024-02-06 15:22 ` [PATCH 2/2] serial: ns16550: add support for half duplex rs485 Pierre-Olivier Huard
  2024-02-08  7:15 ` [PATCH 0/2] serial: ns16550: support " Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Pierre-Olivier Huard @ 2024-02-06 15:22 UTC (permalink / raw)
  To: barebox; +Cc: sebastien.bourdelin, a.fatoum, oss-contrib, Pierre-Olivier Huard

Add possibility to use RTS signal as a Data Enable signal on RS485
transceivers.

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

diff --git a/drivers/serial/serial_ns16550.c b/drivers/serial/serial_ns16550.c
index c3bc79adc2..73d8365dac 100644
--- a/drivers/serial/serial_ns16550.c
+++ b/drivers/serial/serial_ns16550.c
@@ -40,6 +40,9 @@ struct ns16550_priv {
 	void (*write_reg)(struct ns16550_priv *, uint8_t val, unsigned offset);
 	uint8_t (*read_reg)(struct ns16550_priv *, unsigned offset);
 	const char *access_type;
+
+	bool rs485_mode;
+	bool rs485_rts_active_low;
 };
 
 struct ns16550_drvdata {
@@ -265,9 +268,31 @@ static void rpi_init_port(struct console_device *cdev)
  */
 static void ns16550_putc(struct console_device *cdev, char c)
 {
-	/* Loop Doing Nothing */
-	while ((ns16550_read(cdev, lsr) & LSR_THRE) == 0) ;
+	struct ns16550_priv *priv = to_ns16550_priv(cdev);
+
+	/* wait until FIFO can accept at least one byte */
+	while ((ns16550_read(cdev, lsr) & (LSR_THRE)) != (LSR_THRE))
+		;
+
+	if (priv->rs485_mode) {
+		if (priv->rs485_rts_active_low)
+			ns16550_write(cdev, MCR_RTS, mcr);
+		else
+			ns16550_write(cdev, 0, mcr);
+	}
+
 	ns16550_write(cdev, c, thr);
+
+	if (priv->rs485_mode) {
+		/* wait until FIFO is cleared*/
+		while ((ns16550_read(cdev, lsr) & (LSR_EMPTY)) != (LSR_EMPTY))
+			;
+
+		if (priv->rs485_rts_active_low)
+			ns16550_write(cdev, 0, mcr);
+		else
+			ns16550_write(cdev, MCR_RTS, mcr);
+	}
 }
 
 /**
@@ -321,6 +346,10 @@ static void ns16550_probe_dt(struct device *dev, struct ns16550_priv *priv)
 		priv->mmiobase += offset;
 	of_property_read_u32(np, "reg-shift", &priv->plat.shift);
 	of_property_read_u32(np, "reg-io-width", &width);
+	priv->rs485_rts_active_low =
+		of_property_read_bool(np, "rs485-rts-active-low");
+	priv->rs485_mode =
+		of_property_read_bool(np, "linux,rs485-enabled-at-boot-time");
 
 	switch (width) {
 	case 1:
diff --git a/drivers/serial/serial_ns16550.h b/drivers/serial/serial_ns16550.h
index 56c639a134..1c5d9d551a 100644
--- a/drivers/serial/serial_ns16550.h
+++ b/drivers/serial/serial_ns16550.h
@@ -73,6 +73,9 @@
 #define LSR_TEMT	0x40	/* Xmitter empty */
 #define LSR_ERR		0x80	/* Error */
 
+/* Transmitter FIFO completely empty */
+#define LSR_EMPTY	(LSR_THRE | LSR_TEMT)
+
 /* useful defaults for LCR */
 #define LCR_8N1		0x03
 
-- 
2.43.0




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] serial: ns16550: add support for half duplex rs485
  2024-02-06 15:22 [PATCH 0/2] serial: ns16550: support half duplex rs485 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
  2024-02-08  7:15 ` [PATCH 0/2] serial: ns16550: support " Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Pierre-Olivier Huard @ 2024-02-06 15:22 UTC (permalink / raw)
  To: barebox; +Cc: sebastien.bourdelin, a.fatoum, oss-contrib, Pierre-Olivier Huard

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] serial: ns16550: support half duplex rs485
  2024-02-06 15:22 [PATCH 0/2] serial: ns16550: support half duplex rs485 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 ` [PATCH 2/2] serial: ns16550: add support for half duplex rs485 Pierre-Olivier Huard
@ 2024-02-08  7:15 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2024-02-08  7:15 UTC (permalink / raw)
  To: barebox, Pierre-Olivier Huard; +Cc: sebastien.bourdelin, a.fatoum, oss-contrib


On Tue, 06 Feb 2024 16:22:37 +0100, Pierre-Olivier Huard wrote:
> The default ns16550 driver does not support rs485. This patchset adds
> support for half duplex rs485 to the ns16550 driver. Basic handling of the RTS
> signal is done to enable the rs485 transceivers.
> 
> Some properties are now parsed from the device tree:
> - 'linux,rs485-enabled-at-boot-time', enabling RTS toggling when sending data
> - 'rs485-rts-active-low', inverting the RTS signal
> - 'rs485-rx-during-tx', enabling RX during TX, disabled by default
> 
> [...]

Applied, thanks!

[1/2] serial: ns16550: add basic support for rs485
      https://git.pengutronix.de/cgit/barebox/commit/?id=8ce3f15f2125 (link may not be stable)
[2/2] serial: ns16550: add support for half duplex rs485
      https://git.pengutronix.de/cgit/barebox/commit/?id=6530d4bad33d (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-08  7:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-06 15:22 [PATCH 0/2] serial: ns16550: support half duplex rs485 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 ` [PATCH 2/2] serial: ns16550: add support for half duplex rs485 Pierre-Olivier Huard
2024-02-08  7:15 ` [PATCH 0/2] serial: ns16550: support " Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox