linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Heiko Stuebner <heiko@sntech.de>
To: gregkh@linuxfoundation.org
Cc: jslaby@suse.com, andriy.shevchenko@linux.intel.com,
	matwey.kornilov@gmail.com, linux-serial@vger.kernel.org,
	linux-kernel@vger.kernel.org, heiko@sntech.de,
	Heiko Stuebner <heiko.stuebner@theobroma-systems.com>
Subject: [PATCH 2/7] serial: 8250: add serial_in_poll_timeout helper
Date: Wed, 18 Mar 2020 15:26:35 +0100	[thread overview]
Message-ID: <20200318142640.982763-3-heiko@sntech.de> (raw)
In-Reply-To: <20200318142640.982763-1-heiko@sntech.de>

From: Heiko Stuebner <heiko.stuebner@theobroma-systems.com>

In cases where a serial register needs to be polled until a specific
state, this should have a timeout as noted in the thread bringing em485
support to 8250_dw.

To not re-implement timeout handling in each case, add a helper modelled
after readx_poll_timeout / regmap_read_poll_timeout to facilitate this.

Signed-off-by: Heiko Stuebner <heiko.stuebner@theobroma-systems.com>
---
 drivers/tty/serial/8250/8250.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h
index 33ad9d6de532..50a4c174410d 100644
--- a/drivers/tty/serial/8250/8250.h
+++ b/drivers/tty/serial/8250/8250.h
@@ -118,6 +118,40 @@ static inline void serial_out(struct uart_8250_port *up, int offset, int value)
 	up->port.serial_out(&up->port, offset, value);
 }
 
+/**
+ * serial_in_poll_timeout - Poll until a condition is met or a timeout occurs
+ *
+ * @port: uart_8250_port to read from
+ * @offs: Offset to poll
+ * @val: Integer variable to read the value into
+ * @cond: Break condition (usually involving @val)
+ * @timeout_us: Timeout in us, 0 means never timeout
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout or the serial_in
+ * error return value in case of a error read.
+ *
+ * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
+ */
+#define serial_in_poll_timeout(port, offs, val, cond, timeout_us) \
+({ \
+	u64 __timeout_us = (timeout_us); \
+	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
+	for (;;) { \
+		val = serial_in((port), (offs)); \
+		if (val < 0) \
+			break; \
+		if (cond) \
+			break; \
+		if ((__timeout_us) && \
+		    ktime_compare(ktime_get(), __timeout) > 0) { \
+			val = serial_in((port), (offs)); \
+			break; \
+		} \
+		cpu_relax(); \
+	} \
+	(val < 0) ? val : ((cond) ? 0 : -ETIMEDOUT); \
+})
+
 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
 
 static inline int serial_dl_read(struct uart_8250_port *up)
-- 
2.24.1


  parent reply	other threads:[~2020-03-18 14:27 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-18 14:26 [PATCH 0/7] serial: 8250: Add rs485 emulation to 8250_dw Heiko Stuebner
2020-03-18 14:26 ` [PATCH 1/7] serial: 8250: Make em485_rts_after_send() set mctrl according to rts state Heiko Stuebner
2020-03-18 14:26 ` Heiko Stuebner [this message]
2020-03-18 15:09   ` [PATCH 2/7] serial: 8250: add serial_in_poll_timeout helper Andy Shevchenko
2020-03-18 14:26 ` [PATCH 3/7] serial: 8250: Handle case port doesn't have TEMT interrupt using em485 Heiko Stuebner
2020-03-18 14:26 ` [PATCH 4/7] serial: 8250: Start rs485 after registering port if rs485 is enabled in probe Heiko Stuebner
2020-03-18 14:26 ` [PATCH 5/7] serial: 8250: handle DTR in rs485 emulation Heiko Stuebner
2020-03-18 14:26 ` [PATCH 6/7] serial: 8250_dw: add em485 support Heiko Stuebner
2020-03-18 15:15   ` Andy Shevchenko
2020-03-18 14:26 ` [PATCH 7/7] serial: 8250_dw: allow enable rs485 at boot time Heiko Stuebner
2020-03-18 15:16   ` Andy Shevchenko
2020-03-18 18:28     ` Heiko Stübner
2020-03-18 18:31       ` Heiko Stübner
2020-03-18 14:43 ` [PATCH 0/7] serial: 8250: Add rs485 emulation to 8250_dw Andy Shevchenko
2020-03-18 15:37   ` Lukas Wunner
2020-03-18 15:54     ` Andy Shevchenko
2020-03-18 18:49     ` Heiko Stübner
2020-03-19  5:40       ` Lukas Wunner
2020-03-23  8:25         ` Heiko Stübner
2020-03-23 13:17           ` Lukas Wunner
2020-03-23 13:41             ` Andy Shevchenko
2020-03-25 13:41               ` Heiko Stübner
2020-03-25 14:42                 ` Andy Shevchenko
2020-03-18 15:13 ` Andy Shevchenko

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=20200318142640.982763-3-heiko@sntech.de \
    --to=heiko@sntech.de \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heiko.stuebner@theobroma-systems.com \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=matwey.kornilov@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).