Skip to contentSkip to conversation

serial: uapi: add SER_RS485_DELAY_IN_USEC flag to struct serial_rs485

PATCH In review Martin Kepplinger 3 participants 12 messages Feb 21, 2019 – Mar 29, 2019

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/9 serial: uapi: add SER_RS485_DELAY_IN_USEC flag to struct serial_rs485

This extends the user interface for rs485 communication:

We add a new flag, SER_RS485_DELAY_IN_USEC, to struct serial_rs485 that indicates that delay_rts_before_send and delay_rts_after_send values are interpreted in microsecond units.

Up until now, the code comment defined these values to hold the delays in millisecond units. Especially with fast data rates (1Mbaut or more) that are not too uncommon for RS485, 1ms become quite long. Users need to be able to set shorter delays than 1 ms in order not to slow down the channel unnecessarily.

So when delays are needed, but not as long as 1ms, this enables faster communication channels without changing the baudrate.

---

revision history
----------------
v2: re-send as a proper series after fixing my mailserver
v1: initial implementation idea

So have this totally quirky patch that uses udelay() in our tree for a looong time now because of the above reasons - and because we are lazy. This is an attempt to get rid of said patch on our side and fix this properly.

What do you thing about adding a flag in general?

The following patches should integrate this idea in devicetree and drivers. These changes are NOT tested on hardware but should behave predictably enough. I use the delays in a driver that doesn't implement them yet at all. I'll do that after this (or something similar) is merged - it's a 2-liner then.

Also, a patch to the rs485conf tool, that is sometimes used instead of ioctl() directly, will be prepared as well.

thanks

martin

 include/uapi/linux/serial.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
include/uapi/linux/serial.h +9 -2
index 93eb3c496ff1..c16c950ebca2 100644
@@ -126,8 +126,15 @@ struct serial_rs485 {
 #define SER_RS485_TERMINATE_BUS		(1 << 5)	/* Enable bus
 							   termination
 							   (if supported) */
-	__u32	delay_rts_before_send;	/* Delay before send (milliseconds) */
-	__u32	delay_rts_after_send;	/* Delay after send (milliseconds) */
+#define SER_RS485_DELAY_IN_USEC		(1 << 6)	/* delay_rts_*_send
+							   values are given in
+							   microseconds */
+	__u32	delay_rts_before_send;	/* Delay before send (milliseconds
+					   by default. microseconds if flag
+					   is set) */
+	__u32	delay_rts_after_send;	/* Delay after send (milliseconds
+					   by default. microseconds if flag
+					   is set) */
 	__u32	padding[5];		/* Memory is cheap, new structs
 					   are a royal PITA .. */
 };
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 2/9 Documentation: serial-rs485: document SER_RS485_DELAY_IN_USEC flag

Document the new RS485 flag, SER_RS485_DELAY_IN_USEC that specifies that the rts delay values stored in struct serial_rs485 hold values in microseconds instead of milliseconds (the default).

---
 Documentation/serial/serial-rs485.txt | 3 +++
 1 file changed, 3 insertions(+)
Documentation/serial/serial-rs485.txt +3 -0
index ce0c1a9b8aab..a1e15e9efc2e 100644
@@ -75,6 +75,9 @@
 	/* Set rts delay after send, if needed: */
 	rs485conf.delay_rts_after_send = ...;
 
+	/* Specify the rts delay to be microseconds, not milliseconds */
+	rs485conf.flags |= SER_RS485_DELAY_IN_USEC;
+
 	/* Set this flag if you want to receive data even while sending data */
 	rs485conf.flags |= SER_RS485_RX_DURING_TX;
 
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 3/9 serial: core: add rs485-rts-delay-us devicetree property for RS485

In reply to Martin Kepplinger

struct serial_rs485 now optionally holds the rts delay values in microseconds. Users can set these delays in their devicetree descriptions, so this adds the microseconds-option with the "rs485-rts-delay-us" boolean property.

---
 Documentation/devicetree/bindings/serial/rs485.txt |  1 +
 drivers/tty/serial/serial_core.c                   | 11 +++++++++++
 2 files changed, 12 insertions(+)
Documentation/devicetree/bindings/serial/rs485.txt +1 -0
index b92592dff6dd..77396c62b383 100644
@@ -12,6 +12,7 @@ Optional properties:
   * b is the delay between end of data sent and rts signal in milliseconds
       it corresponds to the delay after sending data and actual release of the line.
   If this property is not specified, <0 0> is assumed.
+- rs485-rts-delay-us: the same as rs485-rts-delay, but in microseconds.
 - rs485-rts-active-low: drive RTS low when sending (default is high).
 - linux,rs485-enabled-at-boot-time: empty property telling to enable the rs485
   feature at boot time. It can be disabled later with proper ioctl.
drivers/tty/serial/serial_core.c +11 -0
index 556f50aa1b58..4fb265b2c0fe 100644
@@ -3097,6 +3097,17 @@ void uart_get_rs485_mode(struct device *dev, struct serial_rs485 *rs485conf)
 		rs485conf->delay_rts_after_send = 0;
 	}
 
+	ret = device_property_read_u32_array(dev, "rs485-rts-delay-us",
+					     rs485_delay, 2);
+	if (!ret) {
+		rs485conf->delay_rts_before_send = rs485_delay[0];
+		rs485conf->delay_rts_after_send = rs485_delay[1];
+		rs485conf->flags |= SER_RS485_DELAY_IN_USEC;
+	} else {
+		rs485conf->delay_rts_before_send = 0;
+		rs485conf->delay_rts_after_send = 0;
+	}
+
 	/*
 	 * Clear full-duplex and enabled flags, set RTS polarity to active high
 	 * to get to a defined state with the following properties:
Signature · 1 line
2.20.1
maintainer
#3 lore raw

Re: [PATCH v2 3/9] serial: core: add rs485-rts-delay-us devicetree property for RS485

Quoted 4 lines · Martin Kepplinger

struct serial_rs485 now optionally holds the rts delay values in microseconds. Users can set these delays in their devicetree descriptions, so this adds the microseconds-option with the "rs485-rts-delay-us" boolean property.

If it has a value, it's not boolean.

Should the old prop be deprecated?

Quoted 19 lines
---
 Documentation/devicetree/bindings/serial/rs485.txt |  1 +
 drivers/tty/serial/serial_core.c                   | 11 +++++++++++
 2 files changed, 12 insertions(+)
Documentation/devicetree/bindings/serial/rs485.txt +1 -0
index b92592dff6dd..77396c62b383 100644
@@ -12,6 +12,7 @@ Optional properties:
   * b is the delay between end of data sent and rts signal in milliseconds
       it corresponds to the delay after sending data and actual release of the line.
   If this property is not specified, <0 0> is assumed.
+- rs485-rts-delay-us: the same as rs485-rts-delay, but in microseconds.
 - rs485-rts-active-low: drive RTS low when sending (default is high).
 - linux,rs485-enabled-at-boot-time: empty property telling to enable the rs485
   feature at boot time. It can be disabled later with proper ioctl.
#4 lore raw

PATCH v2 4/9 serial: 8250: add support for rs485 RTS delays in microseconds

In reply to Martin Kepplinger

Read struct serial_rs485's flag SER_RS485_DELAY_IN_USEC and apply the delay accordingly.

---
 drivers/tty/serial/8250/8250_omap.c | 13 +++++++++++--
 drivers/tty/serial/8250/8250_port.c | 25 +++++++++++++++++++++----
 2 files changed, 32 insertions(+), 6 deletions(-)
drivers/tty/serial/8250/8250_omap.c +11 -2
index 0a8316632d75..cbce43ac60b1 100644
@@ -720,8 +720,17 @@ static int omap_8250_rs485_config(struct uart_port *port,
 	struct uart_8250_port *up = up_to_u8250p(port);
 
 	/* Clamp the delays to [0, 100ms] */
-	rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
-	rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send, 100U);
+	if (rs485->flags & SER_RS485_DELAY_IN_USEC) {
+		rs485->delay_rts_before_send = min(rs485->delay_rts_before_send,
+						   100000U);
+		rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send,
+						   100000U);
+	} else {
+		rs485->delay_rts_before_send = min(rs485->delay_rts_before_send,
+						   100U);
+		rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send,
+						   100U);
+	}
 
 	port->rs485 = *rs485;
 
drivers/tty/serial/8250/8250_port.c +21 -4
index d2f3310abe54..0cee4aa8323d 100644
@@ -1483,6 +1483,15 @@ static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
 	hrtimer_start(hrt, t, HRTIMER_MODE_REL);
 }
 
+static void start_hrtimer_us(struct hrtimer *hrt, unsigned long usec)
+{
+	long sec = usec / 1000000;
+	long nsec = (usec % 1000000) * 1000000000;
+	ktime_t t = ktime_set(sec, nsec);
+
+	hrtimer_start(hrt, t, HRTIMER_MODE_REL);
+}
+
 static void __stop_tx_rs485(struct uart_8250_port *p)
 {
 	struct uart_8250_em485 *em485 = p->em485;
@@ -1493,8 +1502,12 @@ static void __stop_tx_rs485(struct uart_8250_port *p)
 	 */
 	if (p->port.rs485.delay_rts_after_send > 0) {
 		em485->active_timer = &em485->stop_tx_timer;
-		start_hrtimer_ms(&em485->stop_tx_timer,
-				   p->port.rs485.delay_rts_after_send);
+		if (p->port.rs485.flags & SER_RS485_DELAY_IN_USEC)
+			start_hrtimer_us(&em485->stop_tx_timer,
+					 p->port.rs485.delay_rts_after_send);
+		else
+			start_hrtimer_ms(&em485->stop_tx_timer,
+					 p->port.rs485.delay_rts_after_send);
 	} else {
 		__do_stop_tx_rs485(p);
 	}
@@ -1600,8 +1613,12 @@ static inline void start_tx_rs485(struct uart_port *port)
 
 		if (up->port.rs485.delay_rts_before_send > 0) {
 			em485->active_timer = &em485->start_tx_timer;
-			start_hrtimer_ms(&em485->start_tx_timer,
-					 up->port.rs485.delay_rts_before_send);
+			if (up->port.rs485.flags & SER_RS485_DELAY_IN_USEC)
+				start_hrtimer_us(&em485->start_tx_timer,
+					up->port.rs485.delay_rts_before_send);
+			else
+				start_hrtimer_ms(&em485->start_tx_timer,
+					up->port.rs485.delay_rts_before_send);
 			return;
 		}
 	}
Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH v2 4/9] serial: 8250: add support for rs485 RTS delays in microseconds

Quoted 38 lines · Martin Kepplinger

Read struct serial_rs485's flag SER_RS485_DELAY_IN_USEC and apply the delay accordingly.

---
 drivers/tty/serial/8250/8250_omap.c | 13 +++++++++++--
 drivers/tty/serial/8250/8250_port.c | 25 +++++++++++++++++++++----
 2 files changed, 32 insertions(+), 6 deletions(-)
drivers/tty/serial/8250/8250_omap.c +11 -2
index 0a8316632d75..cbce43ac60b1 100644
@@ -720,8 +720,17 @@ static int omap_8250_rs485_config(struct uart_port *port,
 	struct uart_8250_port *up = up_to_u8250p(port);
 
 	/* Clamp the delays to [0, 100ms] */
-	rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
-	rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send, 100U);
+	if (rs485->flags & SER_RS485_DELAY_IN_USEC) {
+		rs485->delay_rts_before_send = min(rs485->delay_rts_before_send,
+						   100000U);
+		rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send,
+						   100000U);
+	} else {
+		rs485->delay_rts_before_send = min(rs485->delay_rts_before_send,
+						   100U);
+		rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send,
+						   100U);
+	}
 
 	port->rs485 = *rs485;
 
drivers/tty/serial/8250/8250_port.c +0 -0
index d2f3310abe54..0cee4aa8323d 100644

drivers/tty/serial/8250/8250_port.c static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec) · line 1489

Quoted 7 lines
drivers/tty/serial/8250/8250_port.c +4 -0
@@ -1483,6 +1483,15 @@ static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
 	hrtimer_start(hrt, t, HRTIMER_MODE_REL);
 }
 
+static void start_hrtimer_us(struct hrtimer *hrt, unsigned long usec)
+{
+	long sec = usec / 1000000;
+	long nsec = (usec % 1000000) * 1000000000;

This 1e9 should be 1000 ("NSEC_PER_USEC").
While at it you might want to use USEC_PER_SEC (and MSEC_PER_SEC above) to make the code more readable.

Cheers
Ahmad

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#6 lore raw

PATCH v2 5/9 serial: omap-serial: add support for rs485 RTS delays in microseconds

In reply to Martin Kepplinger

Read struct serial_rs485's flag SER_RS485_DELAY_IN_USEC and apply the delay accordingly.

---
 drivers/tty/serial/omap-serial.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/tty/serial/omap-serial.c +21 -4
index 6420ae581a80..adcd75ce5112 100644
@@ -310,7 +310,11 @@ static void serial_omap_stop_tx(struct uart_port *port)
 			res = (port->rs485.flags & SER_RS485_RTS_AFTER_SEND) ?
 				1 : 0;
 			if (gpio_get_value(up->rts_gpio) != res) {
-				if (port->rs485.delay_rts_after_send > 0)
+				if (port->rs485.delay_rts_after_send > 0 &&
+				    port->rs485.flags & SER_RS485_DELAY_IN_USEC)
+					udelay(
+					port->rs485.delay_rts_after_send);
+				else if (port->rs485.delay_rts_after_send > 0)
 					mdelay(
 					port->rs485.delay_rts_after_send);
 				gpio_set_value(up->rts_gpio, res);
@@ -420,7 +424,11 @@ static void serial_omap_start_tx(struct uart_port *port)
 		res = (port->rs485.flags & SER_RS485_RTS_ON_SEND) ? 1 : 0;
 		if (gpio_get_value(up->rts_gpio) != res) {
 			gpio_set_value(up->rts_gpio, res);
-			if (port->rs485.delay_rts_before_send > 0)
+			if (port->rs485.delay_rts_before_send > 0 &&
+			    port->rs485.flags & SER_RS485_DELAY_IN_USEC)
+				udelay(port->rs485.delay_rts_before_send);
+			else if (port->rs485.delay_rts_before_send > 0 &&
+				 !(port->rs485.flags & SER_RS485_DELAY_IN_USEC)
 				mdelay(port->rs485.delay_rts_before_send);
 		}
 	}
@@ -1407,8 +1415,17 @@ serial_omap_config_rs485(struct uart_port *port, struct serial_rs485 *rs485)
 	serial_out(up, UART_IER, 0);
 
 	/* Clamp the delays to [0, 100ms] */
-	rs485->delay_rts_before_send = min(rs485->delay_rts_before_send, 100U);
-	rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send, 100U);
+	if (port->rs485.flags & SER_RS485_DELAY_IN_USEC) {
+		rs485->delay_rts_before_send = min(rs485->delay_rts_before_send,
+						   100000U);
+		rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send,
+						   100000U);
+	} else {
+		rs485->delay_rts_before_send = min(rs485->delay_rts_before_send,
+						   100);
+		rs485->delay_rts_after_send  = min(rs485->delay_rts_after_send,
+						   100U);
+	}
 
 	/* store new config */
 	port->rs485 = *rs485;
Signature · 1 line
2.20.1
#7 lore raw

PATCH v2 6/9 serial: sc16is7xx: add support for rs485 RTS delays in microseconds

In reply to Martin Kepplinger

Read struct serial_rs485's flag SER_RS485_DELAY_IN_USEC and apply the delay accordingly.

---
 drivers/tty/serial/sc16is7xx.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/tty/serial/sc16is7xx.c +7 -1
index 635178cf3eed..b0e00b9fb177 100644
@@ -743,7 +743,13 @@ static void sc16is7xx_tx_proc(struct kthread_work *ws)
 	struct uart_port *port = &(to_sc16is7xx_one(ws, tx_work)->port);
 
 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
-	    (port->rs485.delay_rts_before_send > 0))
+	    (port->rs485.delay_rts_before_send > 0) &&
+	    (port->rs485.flags & SER_RS485_DELAY_IN_USEC))
+		usleep_range(port->rs485.delay_rts_before_send,
+			     port->rs485.delay_rts_before_send);
+	else if ((port->rs485.flags & SER_RS485_ENABLED) &&
+		 (port->rs485.delay_rts_before_send > 0) &&
+		 !(port->rs485.flags & SER_RS485_DELAY_IN_USEC))
 		msleep(port->rs485.delay_rts_before_send);
 
 	sc16is7xx_handle_tx(port);
Signature · 1 line
2.20.1
#8 lore raw

PATCH v2 7/9 serial: atmel_serial: set SER_RS485_DELAY_IN_USEC accordingly

In reply to Martin Kepplinger

Unset the SER_RS485_DELAY_IN_USEC flag during rs485 config for userspace to get the correct setting.

---
 drivers/tty/serial/atmel_serial.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/serial/atmel_serial.c +3 -0
index 05147fe24343..36ab1c131d36 100644
@@ -346,6 +346,9 @@ static int atmel_config_rs485(struct uart_port *port,
 
 	port->rs485 = *rs485conf;
 
+	/* delays are in milliseconds */
+	rs485conf->flags &= ~SER_RS485_DELAY_IN_USEC;
+
 	if (rs485conf->flags & SER_RS485_ENABLED) {
 		dev_dbg(port->dev, "Setting UART to RS485\n");
 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Signature · 1 line
2.20.1
#9 lore raw

PATCH v2 8/9 serial: fsl_lpuart: set SER_RS485_DELAY_IN_USEC accordingly

In reply to Martin Kepplinger

Clear SER_RS485_DELAY_IN_USEC for userspace to get correct settings.

---
 drivers/tty/serial/fsl_lpuart.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tty/serial/fsl_lpuart.c +1 -0
index ea1c85e3b432..a63aa22e3a25 100644
@@ -1124,6 +1124,7 @@ static int lpuart_config_rs485(struct uart_port *port,
 	rs485->delay_rts_before_send = 0;
 	rs485->delay_rts_after_send = 0;
 	rs485->flags &= ~SER_RS485_RX_DURING_TX;
+	rs485->flags &= ~SER_RS485_DELAY_IN_USEC;
 
 	if (rs485->flags & SER_RS485_ENABLED) {
 		/* Enable auto RS-485 RTS mode */
Signature · 1 line
2.20.1
#10 lore raw

PATCH v2 9/9 serial: st32-usart: set SER_RS485_DELAY_IN_USEC accordingly

In reply to Martin Kepplinger

Unset SER_RS485_DELAY_IN_USEC for userspace to get correct settings.

---
 drivers/tty/serial/stm32-usart.c | 1 +
 1 file changed, 1 insertion(+)
drivers/tty/serial/stm32-usart.c +1 -0
index e8d7a7bb4339..4daf5fc71644 100644
@@ -112,6 +112,7 @@ static int stm32_config_rs485(struct uart_port *port,
 
 	port->rs485 = *rs485conf;
 
+	rs485conf->flags &= ~SER_RS485_DELAY_IN_USEC;
 	rs485conf->flags |= SER_RS485_RX_DURING_TX;
 
 	if (rs485conf->flags & SER_RS485_ENABLED) {
Signature · 1 line
2.20.1
#11 lore raw
In reply to Martin Kepplinger
Quoted 30 lines · Martin Kepplinger

This extends the user interface for rs485 communication:

We add a new flag, SER_RS485_DELAY_IN_USEC, to struct serial_rs485 that indicates that delay_rts_before_send and delay_rts_after_send values are interpreted in microsecond units.

Up until now, the code comment defined these values to hold the delays in millisecond units. Especially with fast data rates (1Mbaut or more) that are not too uncommon for RS485, 1ms become quite long. Users need to be able to set shorter delays than 1 ms in order not to slow down the channel unnecessarily.

So when delays are needed, but not as long as 1ms, this enables faster communication channels without changing the baudrate.

---

revision history
----------------
v2: re-send as a proper series after fixing my mailserver
v1: initial implementation idea

So have this totally quirky patch that uses udelay() in our tree for a looong time now because of the above reasons - and because we are lazy. This is an attempt to get rid of said patch on our side and fix this properly.

What do you thing about adding a flag in general?

Quoted 9 lines

The following patches should integrate this idea in devicetree and drivers. These changes are NOT tested on hardware but should behave predictably enough. I use the delays in a driver that doesn't implement them yet at all. I'll do that after this (or something similar) is merged - it's a 2-liner then.

Also, a patch to the rs485conf tool, that is sometimes used instead of ioctl() directly, will be prepared as well.

any objections of questions about having microsecond delays and this series of changes?

thanks

martin