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 1/2] serial: ns16550: add basic support for rs485
Date: Tue,  6 Feb 2024 16:22:38 +0100	[thread overview]
Message-ID: <20240206152239.341460-2-pierre-olivier.huard@rtone.fr> (raw)
In-Reply-To: <20240206152239.341460-1-pierre-olivier.huard@rtone.fr>

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




  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 half duplex rs485 Pierre-Olivier Huard
2024-02-06 15:22 ` Pierre-Olivier Huard [this message]
2024-02-06 15:22 ` [PATCH 2/2] serial: ns16550: add support for " Pierre-Olivier Huard
2024-02-08  7:15 ` [PATCH 0/2] serial: ns16550: support " 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-2-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