Skip to contentSkip to conversation

gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

PATCH New Fabio Estevam 4 participants 18 messages Sep 28, 2013 – Sep 30, 2013

Subsystems (2)
lore
Share & tools
lore

Fabio Estevam

author reviewer
#0 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

From: Fabio Estevam <[email protected]>

When the GPIO is configured as an output, we should return the value from the DR register.

This implements the same fix as in the following kernel commit from FSL BSP:
http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/commit/arch/arm/plat-mxc/gpio.c?h=imx_3.0.35_4.1.0&id=d6f32393eaf455ce3c32d4e9bafd34d9091eaf45

---
Otavio,

I don't have i.mx hardware access at the moment to try it, but this should fix your LED problem.

 drivers/gpio/mxc_gpio.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpio/mxc_gpio.c +6 -3
index 6a572d5..debbecb 100644
@@ -93,7 +93,7 @@ int gpio_get_value(unsigned gpio)
 {
 	unsigned int port = GPIO_TO_PORT(gpio);
 	struct gpio_regs *regs;
-	u32 val;
+	u32 direction;
 
 	if (port >= ARRAY_SIZE(gpio_ports))
 		return -1;
@@ -102,9 +102,12 @@ int gpio_get_value(unsigned gpio)
 
 	regs = (struct gpio_regs *)gpio_ports[port];
 
-	val = (readl(&regs->gpio_psr) >> gpio) & 0x01;
+	direction = readl(&regs->gpio_dir);
 
-	return val;
+	if ((direction >> gpio) & 1)
+		return (readl(&regs->gpio_dr) >> gpio) & 1; /* output mode */
+	else
+		return (readl(&regs->gpio_psr) >> gpio) & 1; /* input mode */
 }
 
 int gpio_request(unsigned gpio, const char *label)
Signature · 1 line
1.8.1.2
#1 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Quoted 10 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

When the GPIO is configured as an output, we should return the value from the DR register.

This implements the same fix as in the following kernel commit from FSL BSP:
http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/commit/arch/arm/plat-mxc/gpio.c?h=imx_3.0.35_4.1.0&id=d6f32393eaf455ce3c32d4e9bafd34d9091eaf45

---

It does indeed; I redid the series using this and simplified the code.

Thanks by looking at this.

Signature · 3 lines
Otavio Salvador                             O.S. Systems
http://www.ossystems.com.br        http://code.ossystems.com.br
Mobile: +55 (53) 9981-7854            Mobile: +1 (347) 903-9750
#2 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

In reply to Fabio Estevam

Hi Fabio,

Quoted 8 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

When the GPIO is configured as an output, we should return the value from the DR
register.

This implements the same fix as in the following kernel commit from FSL BSP:
http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/commit/arch/arm/plat-mxc/gpio.c?h=imx_3.0.35_4.1.0&id=d6f32393eaf455ce3c32d4e9bafd34d9091eaf45

Why is this required? Is it because there is a different behavior of the PSR register on one of the i.MXs?

See my commit message here:
http://git.denx.de/cgi-bin/gitweb.cgi?p=u-boot.git;a=commitdiff;h=5dafa4543c399d329c7b01df1afa98437861cac0

In case the registers are configured to output some level on a GPIO but there is a level conflict with other hardware, the general assumption about gpio_get_value() would probably be that it returns the actual GPIO level, not the level that the registers try to apply. For the latter, another function accessing DR could be implemented.

One could also argue that such GPIO level conflicts are just the result of a flawed hardware design, and so that normal software should not care about such a case. But it's good to be able to detect hardware issues from software, and this patch seems to change the meaning of gpio_get_value() to cover some hardware issue. Please give more details.

In any case, there should probably be a comment in the code for this function to explain the various issues and how they are addressed.

Best regards,
Beno?t

#3 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Hi Beno?t,

Le Sun, 29 Sep 2013 15:21:52 +0200 (CEST),
Beno?t Th?baudeau <[email protected]> a ?crit :

Quoted 12 lines

Why is this required? Is it because there is a different behavior of the PSR register on one of the i.MXs?

See my commit message here:
http://git.denx.de/cgi-bin/gitweb.cgi?p=u-boot.git;a=commitdiff;h=5dafa4543c399d329c7b01df1afa98437861cac0

In case the registers are configured to output some level on a GPIO but there is a level conflict with other hardware, the general assumption about gpio_get_value() would probably be that it returns the actual GPIO level, not the level that the registers try to apply. For the latter, another function accessing DR could be implemented.

you are right and if that works in the kernel, that should also work in u-boot. It would be interesting to know if the original patch was really fixing a problem as it would be surprising that setting the pin as an input could fix the level sampling problem reliably : Otavio was that tested on real hardware ?

BTW Otavio if you read that email through the ML, your MX server rejects my emails :
<[email protected]>: host mx.ossystems.com.br[66.7.219.172] said:
450 4.1.8 <[email protected]>: Sender address rejected: Domain not found (in reply to RCPT TO command)

Eric

#4 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Quoted 16 lines · Eric B?nard

Hi Beno?t,

Le Sun, 29 Sep 2013 15:21:52 +0200 (CEST),
Beno?t Th?baudeau <[email protected]> a ?crit :

Quoted 12 lines

Why is this required? Is it because there is a different behavior of the PSR register on one of the i.MXs?

See my commit message here:
http://git.denx.de/cgi-bin/gitweb.cgi?p=u-boot.git;a=commitdiff;h=5dafa4543c399d329c7b01df1afa98437861cac0

In case the registers are configured to output some level on a GPIO but there is a level conflict with other hardware, the general assumption about gpio_get_value() would probably be that it returns the actual GPIO level, not the level that the registers try to apply. For the latter, another function accessing DR could be implemented.

Quoted 5 lines

you are right and if that works in the kernel, that should also work in u-boot. It would be interesting to know if the original patch was really fixing a problem as it would be surprising that setting the pin as an input could fix the level sampling problem reliably : Otavio was that tested on real hardware ?

Yes; it did.

Both my original patch (setting it as input) and Fabio's one checking the other register when in output worked fine.

Quoted 5 lines

BTW Otavio if you read that email through the ML, your MX server rejects my emails :
<[email protected]>: host mx.ossystems.com.br[66.7.219.172] said:
450 4.1.8 <[email protected]>: Sender address rejected: Domain not found (in reply to RCPT TO command)

Indeed. I am trying to fix it :-( my bad.

Signature · 3 lines
Otavio Salvador                             O.S. Systems
http://www.ossystems.com.br        http://code.ossystems.com.br
Mobile: +55 (53) 9981-7854            Mobile: +1 (347) 903-9750
#5 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Le Sun, 29 Sep 2013 14:48:32 -0300,
Otavio Salvador <[email protected]> a ?crit :

Quoted 23 lines · Eric B?nard
Quoted 16 lines

Hi Beno?t,

Le Sun, 29 Sep 2013 15:21:52 +0200 (CEST),
Beno?t Th?baudeau <[email protected]> a ?crit :

Quoted 12 lines

Why is this required? Is it because there is a different behavior of the PSR register on one of the i.MXs?

See my commit message here:
http://git.denx.de/cgi-bin/gitweb.cgi?p=u-boot.git;a=commitdiff;h=5dafa4543c399d329c7b01df1afa98437861cac0

In case the registers are configured to output some level on a GPIO but there is a level conflict with other hardware, the general assumption about gpio_get_value() would probably be that it returns the actual GPIO level, not the level that the registers try to apply. For the latter, another function accessing DR could be implemented.

Quoted 5 lines

you are right and if that works in the kernel, that should also work in u-boot. It would be interesting to know if the original patch was really fixing a problem as it would be surprising that setting the pin as an input could fix the level sampling problem reliably : Otavio was that tested on real hardware ?

Quoted 5 lines

Yes; it did.

Both my original patch (setting it as input) and Fabio's one checking the other register when in output worked fine.

on which CPU is that ?
It's strange reading PSR works in the kernel and not in u-boot.

Eric

author reviewer
#6 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Quoted 1 line · Eric B?nard

on which CPU is that ?

Otavio tested it on mx6.

Quoted 1 line

It's strange reading PSR works in the kernel and not in u-boot.

The patch I sent aligns with the kernel behaviour as well:

http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/commit/arch/arm/plat-mxc/gpio.c?h=imx_3.0.35_4.1.0&id=d6f32393eaf455ce3c32d4e9bafd34d9091eaf45

Will investige it in more details tomorrow at FSL.

Regards,

Fabio Estevam

#7 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Hi Fabio,

Le Sun, 29 Sep 2013 15:22:36 -0300,
Fabio Estevam <[email protected]> a ?crit :

Quoted 12 lines · Eric B?nard
Quoted 2 lines

on which CPU is that ?

Otavio tested it on mx6.

Quoted 1 line

It's strange reading PSR works in the kernel and not in u-boot.

The patch I sent aligns with the kernel behaviour as well:

http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/commit/arch/arm/plat-mxc/gpio.c?h=imx_3.0.35_4.1.0&id=d6f32393eaf455ce3c32d4e9bafd34d9091eaf45

mainline kernel doesn't seem to have this fix.
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/gpio/gpio-mxc.c

Eric

author reviewer
#8 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Quoted 11 lines · Eric B?nard

Hi Fabio,

Le Sun, 29 Sep 2013 15:22:36 -0300,
Fabio Estevam <[email protected]> a ?crit :

Quoted 6 lines · Eric B?nard
Quoted 2 lines

on which CPU is that ?

Otavio tested it on mx6.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

It's strange reading PSR works in the kernel and not in u-boot.

The patch I sent aligns with the kernel behaviour as well:

http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/commit/arch/arm/plat-mxc/gpio.c?h=imx_3.0.35_4.1.0&id=d6f32393eaf455ce3c32d4e9bafd34d9091eaf45

mainline kernel doesn't seem to have this fix.
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/gpio/gpio-mxc.c

Yes, I saw that too. Maybe it will also need to be fixed.

First I want to run it on real hardware and understand the issue in more detail.

Regards,

Fabio Estevam

#9 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Hi Fabio,

Quoted 18 lines · Fabio Estevam
Quoted 17 lines · Eric B?nard

Hi Fabio,

Le Sun, 29 Sep 2013 15:22:36 -0300,
Fabio Estevam <[email protected]> a ?crit :

Quoted 12 lines · Eric B?nard
Quoted 2 lines

on which CPU is that ?

Otavio tested it on mx6.

Quoted 1 line

It's strange reading PSR works in the kernel and not in u-boot.

The patch I sent aligns with the kernel behaviour as well:

http://git.freescale.com/git/cgit.cgi/imx/linux-2.6-imx.git/commit/arch/arm/plat-mxc/gpio.c?h=imx_3.0.35_4.1.0&id=d6f32393eaf455ce3c32d4e9bafd34d9091eaf45

Quoted 7 lines
Quoted 2 lines

mainline kernel doesn't seem to have this fix.
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/gpio/gpio-mxc.c

Yes, I saw that too. Maybe it will also need to be fixed.

First I want to run it on real hardware and understand the issue in more detail.

Can you test again without any GPIO patch, but with SION set for this pin in the IOMUXC? According to the reference manual, SION not being set in the IOMUXC is the only reason that would prevent PSR from reading the pin level in GPIO output mode.

Best regards,
Beno?t

author reviewer
#10 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Hi Beno?t,

Quoted 4 lines · Beno?t Th?baudeau

Can you test again without any GPIO patch, but with SION set for this pin in the IOMUXC? According to the reference manual, SION not being set in the IOMUXC is the only reason that would prevent PSR from reading the pin level in GPIO output mode.

Yes, from the feedback from a colleague the SION bit plays a role here:

"The RM does not describe this clear. The fact should be:

PSR for input, DR for output.
PSR can be for output only when SION bit is set (now PSR = DR). "

Regards,

Fabio Estevam

#11 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Hi Fabio,

Quoted 13 lines · Fabio Estevam

Hi Beno?t,

Quoted 7 lines · Beno?t Th?baudeau

Can you test again without any GPIO patch, but with SION set for this pin in the
IOMUXC? According to the reference manual, SION not being set in the IOMUXC is
the only reason that would prevent PSR from reading the pin level in GPIO output
mode.

Quoted 6 lines

Yes, from the feedback from a colleague the SION bit plays a role here:

"The RM does not describe this clear. The fact should be:

PSR for input, DR for output.
PSR can be for output only when SION bit is set (now PSR = DR). "

Yes, that's well explained in the i.MX6Q RM:
"
28.4.2.1 GPIO Read Mode
The programming sequence for reading input signals should be as follows:
1. Configure IOMUX to select GPIO mode (Via IOMUX Controller (IOMUXC) ).
2. Configure GPIO direction register to input (GPIO_GDIR[GDIR] set to 0b).
3. Read value from data register/pad status register.
[...]
NOTE
While the GPIO direction is set to input (GPIO_GDIR = 0), a read access to GPIO_DR does not return GPIO_DR data.
Instead, it returns the GPIO_PSR data, which is the corresponding input signal value.

28.4.2.2 GPIO Write Mode
The programming sequence for driving output signals should be as follows:
1. Configure IOMUX to select GPIO mode (Via IOMUXC), also enable SION if need to read loopback pad value through PSR
2. Configure GPIO direction register to output (GPIO_GDIR[GDIR] set to 1b).
3. Write value to data register (GPIO_DR).
"

So:
- in input mode: DR = PSR = pin,
- in output mode: DR is applied to the pin, and the pin level can be read back through PSR if SION is set (i.e. PSR = DR unless there is a hardware conflict).

Hence, gpio_get_value() should be left unchanged (using PSR in all cases), and SION should be set for all GPIOs in the i.MX6 pin definition header files.

Best regards,
Beno?t

#12 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

On Sun, Sep 29, 2013 at 4:25 PM, Beno?t Th?baudeau <[email protected]> wrote:
...

Quoted 2 lines

Hence, gpio_get_value() should be left unchanged (using PSR in all cases), and SION should be set for all GPIOs in the i.MX6 pin definition header files.

I just does not follow why this preferred against Fabio's proposed patch to read from DR?

Signature · 3 lines
Otavio Salvador                             O.S. Systems
http://www.ossystems.com.br        http://code.ossystems.com.br
Mobile: +55 (53) 9981-7854            Mobile: +1 (347) 903-9750
#13 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Hi Otavio,

Quoted 9 lines · Otavio Salvador

On Sun, Sep 29, 2013 at 4:25 PM, Beno?t Th?baudeau <[email protected]> wrote:
...

Quoted 3 lines

Hence, gpio_get_value() should be left unchanged (using PSR in all cases), and
SION should be set for all GPIOs in the i.MX6 pin definition header files.

I just does not follow why this preferred against Fabio's proposed patch to read from DR?

Because in case of a level conflict between the GPIO output and some other hardware on the board, one would expect gpio_get_value() to return the actual pin level, and not the level that the GPIO output tries (but possibly fails) to apply on this pin.

Best regards,
Beno?t

#14 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Quoted 8 lines · Beno?t Th?baudeau
Quoted 7 lines · Otavio Salvador

On Sun, Sep 29, 2013 at 4:25 PM, Beno?t Th?baudeau <[email protected]> wrote:
...

Quoted 3 lines

Hence, gpio_get_value() should be left unchanged (using PSR in all cases), and
SION should be set for all GPIOs in the i.MX6 pin definition header files.

Quoted 7 lines
Quoted 2 lines

I just does not follow why this preferred against Fabio's proposed patch to read from DR?

Because in case of a level conflict between the GPIO output and some other hardware on the board, one would expect gpio_get_value() to return the actual pin level, and not the level that the GPIO output tries (but possibly fails) to apply on this pin.

Ahh now I see. I agree :-)

Signature · 3 lines
Otavio Salvador                             O.S. Systems
http://www.ossystems.com.br        http://code.ossystems.com.br
Mobile: +55 (53) 9981-7854            Mobile: +1 (347) 903-9750
#15 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Quoted 13 lines · Otavio Salvador
Quoted 11 lines · Beno?t Th?baudeau
Quoted 9 lines · Otavio Salvador

On Sun, Sep 29, 2013 at 4:25 PM, Beno?t Th?baudeau <[email protected]> wrote:
...

Quoted 3 lines

Hence, gpio_get_value() should be left unchanged (using PSR in all cases), and
SION should be set for all GPIOs in the i.MX6 pin definition header files.

I just does not follow why this preferred against Fabio's proposed patch to read from DR?

Quoted 6 lines
Quoted 4 lines

Because in case of a level conflict between the GPIO output and some other hardware on the board, one would expect gpio_get_value() to return the actual pin level, and not the level that the GPIO output tries (but possibly fails) to apply on this pin.

Ahh now I see. I agree :-)

I sent the patch to fix this adding the flag to the GPIO pins.

I tested it and it works fine indeed. The patch is awaiting for approval as it is a little big. The commitlog is below for reference:

mx6: Add IOMUX_CONFIG_SION flag to all GPIO pins

The IOMUX_CONFIG_SION allows for reading PAD value from PSR register.

The following quote from the datasheet:

,----
| ...
| 28.4.2.2 GPIO Write Mode
| The programming sequence for driving output signals should be as follows:
| 1. Configure IOMUX to select GPIO mode (Via IOMUXC), also enable
SION if need
| to read loopback pad value through PSR
| 2. Configure GPIO direction register to output (GPIO_GDIR[GDIR]
set to 1b).
| 3. Write value to data register (GPIO_DR).
| ...
`----

This fixes the gpio_get_value to properly work when a GPIO is set for output and has no conflicts.

Thanks for Beno?t Th?baudeau <[email protected]>, Fabio Estevam <[email protected]> and Eric B?nard <[email protected]> for helping to properly trace this down.

Again, thanks for the help on this.

Signature · 3 lines
Otavio Salvador                             O.S. Systems
http://www.ossystems.com.br        http://code.ossystems.com.br
Mobile: +55 (53) 9981-7854            Mobile: +1 (347) 903-9750
author reviewer
#16 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Quoted 6 lines · Otavio Salvador

I sent the patch to fix this adding the flag to the GPIO pins.

I tested it and it works fine indeed. The patch is awaiting for approval as it is a little big. The commitlog is below for reference:

mx6: Add IOMUX_CONFIG_SION flag to all GPIO pins

Ok, this fixes mx6, but other i.mx SoC would need the same fix as well, right?

Also, if you have a chance please send a fix for the mainline kernel.

Regards,

Fabio Estevam

#17 lore raw

PATCH 1/1 gpio: mxc_gpio: Fix gpio_get_value() when the GPIO is an output

Hi Fabio,

Quoted 5 lines · Fabio Estevam
Quoted 2 lines · Otavio Salvador

I sent the patch to fix this adding the flag to the GPIO pins.

Quoted 7 lines
Quoted 4 lines

I tested it and it works fine indeed. The patch is awaiting for approval as it is a little big. The commitlog is below for reference:

mx6: Add IOMUX_CONFIG_SION flag to all GPIO pins

Ok, this fixes mx6, but other i.mx SoC would need the same fix as well, right?

Maybe not. The requirement for SION setting for the various pin functions differs among i.MXs. E.g., SION is required for I?C on some i.MX (51 I think), but not for all. So this should be checked for GPIO as well on a per-i.MX basis.

[...]

Best regards,
Beno?t