Skip to contentSkip to conversation

serial: mctrl_gpio: Check if GPIO property exisits before requesting it

PATCH Superseded Stefan Roese 2 participants 5 messages Jun 3, 2019 – Jun 5, 2019

Subsystems (3)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 serial: mctrl_gpio: Check if GPIO property exisits before requesting it

This patch adds a check for the GPIOs property existence, before the GPIO is requested. This fixes an issue seen when the 8250 mctrl_gpio support is added (2nd patch in this patch series) on x86 platforms using ACPI.

Here Mika's comments from 2016-08-09:

"
I noticed that with v4.8-rc1 serial console of some of our Broxton systems does not work properly anymore. I'm able to see output but input does not work.

I bisected it down to commit 4ef03d328769eddbfeca1f1c958fdb181a69c341 ("tty/serial/8250: use mctrl_gpio helpers").

The reason why it fails is that in ACPI we do not have names for GPIOs (except when _DSD is used) so we use the "idx" to index into _CRS GPIO resources. Now mctrl_gpio_init_noauto() goes through a list of GPIOs calling devm_gpiod_get_index_optional() passing "idx" of 0 for each. The UART device in Broxton has following (simplified) ACPI description:

    Device (URT4)
    {
        ...
        Name (_CRS, ResourceTemplate () {
            GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly,
                    "\\_SB.GPO0", 0x00, ResourceConsumer)
            {
                0x003A
            }
            GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly,
                    "\\_SB.GPO0", 0x00, ResourceConsumer)
            {
                0x003D
            }
        })

In this case it finds the first GPIO (0x003A which happens to be RX pin for that UART), turns it into GPIO which then breaks input for the UART device. This also breaks systems with bluetooth connected to UART (those typically have some GPIOs in their _CRS).

Any ideas how to fix this?

We cannot just drop the _CRS index lookup fallback because that would break many existing machines out there so maybe we can limit this to only DT enabled machines. Or alternatively probe if the property first exists before trying to acquire the GPIOs (using device_property_present()).
"

This patch implements the fix suggested by Mika in his statement above.

Cc Mika Westerberg <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Yegor Yefremov <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Giulio Benetti <[email protected]>
Cc Johan Hovold <[email protected]>

---
v4:
- Add missing free() calls (Johan)
- Added Mika's reviewed by tag
- Added Johan to Cc

v3:
- No change

v2:
- Include the problem description and analysis from Mika into the commit text, as suggested by Greg.

 drivers/tty/serial/serial_mctrl_gpio.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/tty/serial/serial_mctrl_gpio.c +10 -0
index 39ed56214cd3..6367f389cdfc 100644
@@ -116,6 +116,16 @@ struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx)
 
 	for (i = 0; i < UART_GPIO_MAX; i++) {
 		enum gpiod_flags flags;
+		char *gpio_str;
+
+		/* Check if GPIO property exists and continue if not */
+		gpio_str = kasprintf(GFP_KERNEL, "%s-gpios",
+				     mctrl_gpios_desc[i].name);
+		if (!device_property_present(dev, gpio_str)) {
+			kfree(gpio_str);
+			continue;
+		}
+		kfree(gpio_str);
 
 		if (mctrl_gpios_desc[i].dir_out)
 			flags = GPIOD_OUT_LOW;
Signature · 1 line
2.21.0
#1 lore raw

PATCH v4 2/2 tty/serial/8250: use mctrl_gpio helpers

From: Yegor Yefremov <[email protected]>

This patch permits the usage for GPIOs to control
the CTS/RTS/DTR/DSR/DCD/RI signals.

Changed by Stefan:
Only call mctrl_gpio_init(), if the device has no ACPI companion device to not break existing ACPI based systems. Also only use the mctrl_gpio_ functions when "gpios" is available.

Cc Mika Westerberg <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Giulio Benetti <[email protected]>
Cc Yegor Yefremov <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Johan Hovold <[email protected]>

---
v4:
- Added Mika's reviewed by tag
- Added Johan to Cc

v3:
- Only call mctrl_gpio_init(), if the device has no ACPI companion device to not break existing ACPI based systems, as suggested by Andy

v2:
- No change

Please note that this patch was already applied before [1]. And later reverted [2] because it introduced problems on some x86 based boards (ACPI GPIO related). Here a detailed description of the issue at that time:

https://lkml.org/lkml/2016/8/9/357
http://www.spinics.net/lists/linux-serial/msg23071.html

This is a re-send of the original patch that was applied at that time. With patch 1/2 from this series this issue should be fixed now (please note that I can't test it on such an x86 platform causing these problems).

Andy (or Mika), perhaps it would be possible for you to test this patch again, now with patch 1/2 of this series applied as well? That would be really helpful.

Thanks,
Stefan

[1] 4ef03d328769 ("tty/serial/8250: use mctrl_gpio helpers")
[2] 5db4f7f80d16 ("Revert "tty/serial/8250: use mctrl_gpio helpers"")

 .../devicetree/bindings/serial/8250.txt       | 19 +++++++++
 drivers/tty/serial/8250/8250.h                | 40 ++++++++++++++++++-
 drivers/tty/serial/8250/8250_core.c           | 17 ++++++++
 drivers/tty/serial/8250/8250_omap.c           | 31 ++++++++------
 drivers/tty/serial/8250/8250_port.c           |  9 +++++
 drivers/tty/serial/8250/Kconfig               |  1 +
 include/linux/serial_8250.h                   |  1 +
 7 files changed, 104 insertions(+), 14 deletions(-)
Documentation/devicetree/bindings/serial/8250.txt +19 -0
index 3cba12f855b7..20d351f268ef 100644
@@ -53,6 +53,9 @@ Optional properties:
   programmable TX FIFO thresholds.
 - resets : phandle + reset specifier pairs
 - overrun-throttle-ms : how long to pause uart rx when input overrun is encountered.
+- {rts,cts,dtr,dsr,rng,dcd}-gpios: specify a GPIO for RTS/CTS/DTR/DSR/RI/DCD
+  line respectively. It will use specified GPIO instead of the peripheral
+  function pin for the UART feature. If unsure, don't specify this property.
 
 Note:
 * fsl,ns16550:
@@ -74,3 +77,19 @@ Example:
 		interrupts = <10>;
 		reg-shift = <2>;
 	};
+
+Example for OMAP UART using GPIO-based modem control signals:
+
+	uart4: serial@49042000 {
+		compatible = "ti,omap3-uart";
+		reg = <0x49042000 0x400>;
+		interrupts = <80>;
+		ti,hwmods = "uart4";
+		clock-frequency = <48000000>;
+		cts-gpios = <&gpio3 5 GPIO_ACTIVE_LOW>;
+		rts-gpios = <&gpio3 6 GPIO_ACTIVE_LOW>;
+		dtr-gpios = <&gpio1 12 GPIO_ACTIVE_LOW>;
+		dsr-gpios = <&gpio1 13 GPIO_ACTIVE_LOW>;
+		dcd-gpios = <&gpio1 14 GPIO_ACTIVE_LOW>;
+		rng-gpios = <&gpio1 15 GPIO_ACTIVE_LOW>;
+	};
drivers/tty/serial/8250/8250.h +39 -1
index ebfb0bd5bef5..441aab94264b 100644
@@ -11,6 +11,8 @@
 #include <linux/serial_reg.h>
 #include <linux/dmaengine.h>
 
+#include "../serial_mctrl_gpio.h"
+
 struct uart_8250_dma {
 	int (*tx_dma)(struct uart_8250_port *p);
 	int (*rx_dma)(struct uart_8250_port *p);
@@ -142,11 +144,47 @@ void serial8250_em485_destroy(struct uart_8250_port *p);
 static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
 {
 	serial_out(up, UART_MCR, value);
+
+	if (up->gpios) {
+		int mctrl_gpio = 0;
+
+		if (value & UART_MCR_RTS)
+			mctrl_gpio |= TIOCM_RTS;
+		if (value & UART_MCR_DTR)
+			mctrl_gpio |= TIOCM_DTR;
+
+		mctrl_gpio_set(up->gpios, mctrl_gpio);
+	}
 }
 
 static inline int serial8250_in_MCR(struct uart_8250_port *up)
 {
-	return serial_in(up, UART_MCR);
+	int mctrl;
+
+	mctrl = serial_in(up, UART_MCR);
+
+	if (up->gpios) {
+		int mctrl_gpio = 0;
+
+		/* save current MCR values */
+		if (mctrl & UART_MCR_RTS)
+			mctrl_gpio |= TIOCM_RTS;
+		if (mctrl & UART_MCR_DTR)
+			mctrl_gpio |= TIOCM_DTR;
+
+		mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
+		if (mctrl_gpio & TIOCM_RTS)
+			mctrl |= UART_MCR_RTS;
+		else
+			mctrl &= ~UART_MCR_RTS;
+
+		if (mctrl_gpio & TIOCM_DTR)
+			mctrl |= UART_MCR_DTR;
+		else
+			mctrl &= ~UART_MCR_DTR;
+	}
+
+	return mctrl;
 }
 
 #if defined(__alpha__) && !defined(CONFIG_PCI)
drivers/tty/serial/8250/8250_core.c +17 -0
index e441221e04b9..a4470771005f 100644
@@ -14,6 +14,7 @@
  *	      serial8250_register_8250_port() ports
  */
 
+#include <linux/acpi.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/ioport.h>
@@ -982,6 +983,8 @@ int serial8250_register_8250_port(struct uart_8250_port *up)
 
 	uart = serial8250_find_match_or_unused(&up->port);
 	if (uart && uart->port.type != PORT_8250_CIR) {
+		struct mctrl_gpios *gpios;
+
 		if (uart->port.dev)
 			uart_remove_one_port(&serial8250_reg, &uart->port);
 
@@ -1016,6 +1019,20 @@ int serial8250_register_8250_port(struct uart_8250_port *up)
 		if (up->port.flags & UPF_FIXED_TYPE)
 			uart->port.type = up->port.type;
 
+		/*
+		 * Only call mctrl_gpio_init(), if the device has no ACPI
+		 * companion device
+		 */
+		if (!has_acpi_companion(uart->port.dev)) {
+			gpios = mctrl_gpio_init(&uart->port, 0);
+			if (IS_ERR(gpios)) {
+				if (PTR_ERR(gpios) != -ENOSYS)
+					return PTR_ERR(gpios);
+			} else {
+				uart->gpios = gpios;
+			}
+		}
+
 		serial8250_set_defaults(uart);
 
 		/* Possibly override default I/O functions.  */
drivers/tty/serial/8250/8250_omap.c +18 -13
index 0a8316632d75..562ac73d9f2f 100644
@@ -141,18 +141,21 @@ static void omap8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
 
 	serial8250_do_set_mctrl(port, mctrl);
 
-	/*
-	 * Turn off autoRTS if RTS is lowered and restore autoRTS setting
-	 * if RTS is raised
-	 */
-	lcr = serial_in(up, UART_LCR);
-	serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
-	if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
-		priv->efr |= UART_EFR_RTS;
-	else
-		priv->efr &= ~UART_EFR_RTS;
-	serial_out(up, UART_EFR, priv->efr);
-	serial_out(up, UART_LCR, lcr);
+	if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(up->gpios,
+						UART_GPIO_RTS))) {
+		/*
+		 * Turn off autoRTS if RTS is lowered and restore autoRTS
+		 * setting if RTS is raised
+		 */
+		lcr = serial_in(up, UART_LCR);
+		serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
+		if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
+			priv->efr |= UART_EFR_RTS;
+		else
+			priv->efr &= ~UART_EFR_RTS;
+		serial_out(up, UART_EFR, priv->efr);
+		serial_out(up, UART_LCR, lcr);
+	}
 }
 
 /*
@@ -453,7 +456,9 @@ static void omap_8250_set_termios(struct uart_port *port,
 	priv->efr = 0;
 	up->port.status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);
 
-	if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
+	if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW
+		&& IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(up->gpios,
+							UART_GPIO_RTS))) {
 		/* Enable AUTOCTS (autoRTS is enabled when RTS is raised) */
 		up->port.status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
 		priv->efr |= UART_EFR_CTS;
drivers/tty/serial/8250/8250_port.c +9 -0
index 2304a84eee3b..1925e62f05bf 100644
@@ -1662,6 +1662,9 @@ static void serial8250_disable_ms(struct uart_port *port)
 	if (up->bugs & UART_BUG_NOMSR)
 		return;
 
+	if (up->gpios)
+		mctrl_gpio_disable_ms(up->gpios);
+
 	up->ier &= ~UART_IER_MSI;
 	serial_port_out(port, UART_IER, up->ier);
 }
@@ -1674,6 +1677,9 @@ static void serial8250_enable_ms(struct uart_port *port)
 	if (up->bugs & UART_BUG_NOMSR)
 		return;
 
+	if (up->gpios)
+		mctrl_gpio_enable_ms(up->gpios);
+
 	up->ier |= UART_IER_MSI;
 
 	serial8250_rpm_get(up);
@@ -1951,6 +1957,9 @@ unsigned int serial8250_do_get_mctrl(struct uart_port *port)
 	serial8250_rpm_put(up);
 
 	ret = 0;
+	if (up->gpios)
+		return mctrl_gpio_get(up->gpios, &ret);
+
 	if (status & UART_MSR_DCD)
 		ret |= TIOCM_CAR;
 	if (status & UART_MSR_RI)
drivers/tty/serial/8250/Kconfig +1 -0
index 296115f6a4d8..509f6a3bb9ff 100644
@@ -8,6 +8,7 @@ config SERIAL_8250
 	tristate "8250/16550 and compatible serial support"
 	depends on !S390
 	select SERIAL_CORE
+	select SERIAL_MCTRL_GPIO if GPIOLIB
 	---help---
 	  This selects whether you want to include the driver for the standard
 	  serial ports.  The standard answer is Y.  People who might say N
include/linux/serial_8250.h +1 -0
index 5e0b59422a68..bb2bc99388ca 100644
@@ -110,6 +110,7 @@ struct uart_8250_port {
 						 *   if no_console_suspend
 						 */
 	unsigned char		probe;
+	struct mctrl_gpios	*gpios;
 #define UART_PROBE_RSA	(1 << 0)
 
 	/*
Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH 2/2 v4] tty/serial/8250: use mctrl_gpio helpers

Quoted 9 lines · Stefan Roese

From: Yegor Yefremov <[email protected]>

This patch permits the usage for GPIOs to control
the CTS/RTS/DTR/DSR/DCD/RI signals.

drivers/tty/serial/8250/8250.h +3 -0
+	if (up->gpios) {
 
+		mctrl_gpio_set(up->gpios, mctrl_gpio);
+	}

...

Quoted 5 lines
drivers/tty/serial/8250/8250.h +3 -0
+	if (up->gpios) {
 
+		mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
 
+	}

...

Quoted 5 lines
drivers/tty/serial/8250/8250_core.c +5 -0
+			gpios = mctrl_gpio_init(&uart->port, 0);
+			if (IS_ERR(gpios)) {
+				if (PTR_ERR(gpios) != -ENOSYS)
+					return PTR_ERR(gpios);
+			}

...

Quoted 4 lines
drivers/tty/serial/8250/8250_omap.c +3 -0
+	if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(up->gpios,
+						UART_GPIO_RTS))) {
 
+	}

...

Quoted 6 lines
drivers/tty/serial/8250/8250_omap.c +3 -1
-	if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
+	if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW
+		&& IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(up->gpios,
+							UART_GPIO_RTS))) {

}

...

Quoted 2 lines
drivers/tty/serial/8250/8250_port.c +2 -0
+	if (up->gpios)
+		mctrl_gpio_disable_ms(up->gpios);

...

Quoted 2 lines
drivers/tty/serial/8250/8250_port.c +2 -0
+	if (up->gpios)
+		mctrl_gpio_enable_ms(up->gpios);

...

drivers/tty/serial/8250/8250_port.c unsigned int serial8250_do_get_mctrl(struct uart_port *port) · line 1961

Quoted 2 lines
drivers/tty/serial/8250/8250_port.c +2 -0
+	if (up->gpios)
+		return mctrl_gpio_get(up->gpios, &ret);

Can we rather make this mimic the gpiod_get_optional() API?

So, if we get an error, it's an error, otherwise with NULL pointer the operations goes to be no-op.

[IS_ERR_OR_NULL() -> IS_ERR(), if (up->gpios) -> /dev/null, etc]

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH 2/2 v4] tty/serial/8250: use mctrl_gpio helpers

Quoted 61 lines · Andy Shevchenko
Quoted 9 lines · Stefan Roese

From: Yegor Yefremov <[email protected]>

This patch permits the usage for GPIOs to control
the CTS/RTS/DTR/DSR/DCD/RI signals.

drivers/tty/serial/8250/8250.h +3 -0
+	if (up->gpios) {
 
+		mctrl_gpio_set(up->gpios, mctrl_gpio);
+	}

...

Quoted 5 lines
drivers/tty/serial/8250/8250.h +3 -0
+	if (up->gpios) {
 
+		mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
 
+	}

...

Quoted 5 lines
drivers/tty/serial/8250/8250_core.c +5 -0
+			gpios = mctrl_gpio_init(&uart->port, 0);
+			if (IS_ERR(gpios)) {
+				if (PTR_ERR(gpios) != -ENOSYS)
+					return PTR_ERR(gpios);
+			}

...

Quoted 4 lines
drivers/tty/serial/8250/8250_omap.c +3 -0
+	if (IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(up->gpios,
+						UART_GPIO_RTS))) {
 
+	}

...

Quoted 6 lines
drivers/tty/serial/8250/8250_omap.c +3 -1
-	if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW) {
+	if (termios->c_cflag & CRTSCTS && up->port.flags & UPF_HARD_FLOW
+		&& IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(up->gpios,
+							UART_GPIO_RTS))) {

}

...

Quoted 2 lines
drivers/tty/serial/8250/8250_port.c +2 -0
+	if (up->gpios)
+		mctrl_gpio_disable_ms(up->gpios);

...

Quoted 2 lines
drivers/tty/serial/8250/8250_port.c +2 -0
+	if (up->gpios)
+		mctrl_gpio_enable_ms(up->gpios);

...

drivers/tty/serial/8250/8250_port.c unsigned int serial8250_do_get_mctrl(struct uart_port *port) · line 1961

Quoted 2 lines
drivers/tty/serial/8250/8250_port.c +2 -0
+	if (up->gpios)
+		return mctrl_gpio_get(up->gpios, &ret);

Can we rather make this mimic the gpiod_get_optional() API?

Quoted 4 lines

So, if we get an error, it's an error, otherwise with NULL pointer the operations goes to be no-op.

[IS_ERR_OR_NULL() -> IS_ERR(), if (up->gpios) -> /dev/null, etc]

So you want me to drop all "if (up->gpios)" checks? I can do this in some cases (e.g. serial8250_disable_ms()). But I would like to keep it in other cases, like serial8250_out_MCR(), where this check prevents some unnecessary code execution in the "non-gpios mode" (and vice-versa).

Would this be acceptable?

BTW: Regarding the OMAP specific code: I'm not the author of this code and I don't have access to such hardware to do some tests here. But changing IS_ERR_OR_NULL() -> IS_ERR() in this OMAP code does not seem correct. IIUTC, these "if" clauses are extended here by IS_ERR_OR_NULL(mctrl_gpio_to_gpiod()) to check if the GPIO's are not enabled / used. Currently this will probably break, since when called with "gpios == NULL", mctrl_gpio_to_gpiod() will crash [1].

If you don't object (or have other suggestions), I'll change this to use "up->gpios == 0" instead. This seems to be what the original author wanted to achieve.

Okay?

Thanks,
Stefan

[1]

struct gpio_desc *mctrl_gpio_to_gpiod(struct mctrl_gpios *gpios,
enum mctrl_gpio_idx gidx)

{
	return gpios->gpio[gidx];
}
#4 lore raw
In reply to Stefan Roese
Quoted 7 lines · Stefan Roese

This patch adds a check for the GPIOs property existence, before the GPIO is requested. This fixes an issue seen when the 8250 mctrl_gpio support is added (2nd patch in this patch series) on x86 platforms using ACPI.

drivers/tty/serial/serial_mctrl_gpio.c +0 -0
 	for (i = 0; i < UART_GPIO_MAX; i++) {
 		enum gpiod_flags flags;

drivers/tty/serial/serial_mctrl_gpio.c struct mctrl_gpios *mctrl_gpio_init_noauto(struct device *dev, unsigned int idx) · line 128

Quoted 10 lines
drivers/tty/serial/serial_mctrl_gpio.c +10 -0
+		char *gpio_str;
+
+		/* Check if GPIO property exists and continue if not */
+		gpio_str = kasprintf(GFP_KERNEL, "%s-gpios",
+				     mctrl_gpios_desc[i].name);
+		if (!device_property_present(dev, gpio_str)) {
+			kfree(gpio_str);
+			continue;
+		}
+		kfree(gpio_str);

Can we rather do something like

	bool present;


	... = kasprintf(...);
// (1)
	present = device_property_present(...);
	kfree(...);
	if (!present)
		continue;

?

On top of this, if gpio_str is NULL, we will have KABOOM.

Something like

	if (!gpio_str)
		continue;

in (1).

Quoted 3 lines
drivers/tty/serial/serial_mctrl_gpio.c +0 -0
 		if (mctrl_gpios_desc[i].dir_out)
 			flags = GPIOD_OUT_LOW;
Signature · 2 lines
With Best Regards,
Andy Shevchenko