Skip to contentSkip to conversation

gpio: mpc8xxx: honour shadow register when writing gpdat

PATCH RESEND Superseded Rasmus Villemoes 2 participants 10 messages Jan 28, 2020 – Mar 31, 2020 GPIO

lore
Share & tools
lore
#0 lore raw

cover gpio: mpc8xxx: honour shadow register when writing gpdat

[resending with Mario's correct address, sorry for the double post]

The driver correctly uses the shadow register when asked for the current value of an output gpio. Unfortunately, it does RMW on the gpdat register both when setting a gpio as input and output. These two patches fix that.

Aside: Apparently, the mpc8309 also partially suffers from the errata preventing outputs from being read back - the bits corresponding to gpios 0-7 are always read as 0, while at least the value of gpio10 is correctly reflected when reading gpdat. Which is how I noticed these bugs - I couldn't understand why turning one LED on would turn off another.

Rasmus Villemoes (2):
gpio: mpc8xxx: don't modify gpdat when setting gpio as input
gpio: mpc8xxx: don't do RMW on gpdat register when setting value

 drivers/gpio/mpc8xxx_gpio.c | 41 ++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 26 deletions(-)
Signature · 1 line
2.23.0
#1 lore raw

PATCH RESEND 1/2 gpio: mpc8xxx: don't modify gpdat when setting gpio as input

Since some chips don't support reading back the value of output gpios from the gpdat register, we should not do a RMW cycle (i.e., the clrbits_be32) on the gpdat register when setting a gpio as input, as that might accidentally change the value of some other (still configured as output) gpio.

The extra indirection through mpc8xxx_gpio_set_in() does not help readability, so just fold the gpdir update into mpc8xxx_gpio_direction_input().

---
 drivers/gpio/mpc8xxx_gpio.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/gpio/mpc8xxx_gpio.c +4 -8
index 9964d69035..5438196fe0 100644
@@ -57,13 +57,6 @@ static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask)
 	return in_be32(&base->gpdir) & mask;
 }
 
-static inline void mpc8xxx_gpio_set_in(struct ccsr_gpio *base, u32 gpios)
-{
-	clrbits_be32(&base->gpdat, gpios);
-	/* GPDIR register 0 -> input */
-	clrbits_be32(&base->gpdir, gpios);
-}
-
 static inline void mpc8xxx_gpio_set_low(struct ccsr_gpio *base, u32 gpios)
 {
 	clrbits_be32(&base->gpdat, gpios);
@@ -100,8 +93,11 @@ static inline void mpc8xxx_gpio_open_drain_off(struct ccsr_gpio *base,
 static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
 {
 	struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
+	u32 mask = gpio_mask(gpio);
+
+	/* GPDIR register 0 -> input */
+	clrbits_be32(&data->base->gpdir, mask);
 
-	mpc8xxx_gpio_set_in(data->base, gpio_mask(gpio));
 	return 0;
 }
 
Signature · 1 line
2.23.0
#2 lore raw

PATCH RESEND 1/2 gpio: mpc8xxx: don't modify gpdat when setting gpio as input

Quoted 11 lines · Rasmus Villemoes

Since some chips don't support reading back the value of output gpios from the gpdat register, we should not do a RMW cycle (i.e., the clrbits_be32) on the gpdat register when setting a gpio as input, as that might accidentally change the value of some other (still configured as output) gpio.

The extra indirection through mpc8xxx_gpio_set_in() does not help readability, so just fold the gpdir update into mpc8xxx_gpio_direction_input().

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200331/980ef6a0/attachment.sig>
#3 lore raw

PATCH RESEND 2/2 gpio: mpc8xxx: don't do RMW on gpdat register when setting value

In reply to Rasmus Villemoes

The driver correctly handles reading back the value of an output gpio by reading from the shadow register for output, and from gpdat for inputs.

Unfortunately, when setting the value of some gpio, we do a RMW cycle on the gpdat register without taking the shadow register into account, thus accidentally setting other output gpios (at least those whose value cannot be read back) to 0 at the same time.

When changing a gpio from input to output, we still need to make sure it initially has the requested value. So, the procedure is

- update the shadow register
- compute the new gpdir register
- write the bitwise and of the shadow and new gpdir register to gpdat
- write the new gpdir register

---
 drivers/gpio/mpc8xxx_gpio.c | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)
drivers/gpio/mpc8xxx_gpio.c +11 -18
index 5438196fe0..c5be8e673b 100644
@@ -57,20 +57,6 @@ static inline u32 mpc8xxx_gpio_get_dir(struct ccsr_gpio *base, u32 mask)
 	return in_be32(&base->gpdir) & mask;
 }
 
-static inline void mpc8xxx_gpio_set_low(struct ccsr_gpio *base, u32 gpios)
-{
-	clrbits_be32(&base->gpdat, gpios);
-	/* GPDIR register 1 -> output */
-	setbits_be32(&base->gpdir, gpios);
-}
-
-static inline void mpc8xxx_gpio_set_high(struct ccsr_gpio *base, u32 gpios)
-{
-	setbits_be32(&base->gpdat, gpios);
-	/* GPDIR register 1 -> output */
-	setbits_be32(&base->gpdir, gpios);
-}
-
 static inline int mpc8xxx_gpio_open_drain_val(struct ccsr_gpio *base, u32 mask)
 {
 	return in_be32(&base->gpodr) & mask;
@@ -104,14 +90,21 @@ static int mpc8xxx_gpio_direction_input(struct udevice *dev, uint gpio)
 static int mpc8xxx_gpio_set_value(struct udevice *dev, uint gpio, int value)
 {
 	struct mpc8xxx_gpio_data *data = dev_get_priv(dev);
+	struct ccsr_gpio *base = data->base;
+	u32 mask = gpio_mask(gpio);
+	u32 gpdir;
 
 	if (value) {
-		data->dat_shadow |= gpio_mask(gpio);
-		mpc8xxx_gpio_set_high(data->base, gpio_mask(gpio));
+		data->dat_shadow |= mask;
 	} else {
-		data->dat_shadow &= ~gpio_mask(gpio);
-		mpc8xxx_gpio_set_low(data->base, gpio_mask(gpio));
+		data->dat_shadow &= ~mask;
 	}
+
+	gpdir = in_be32(&base->gpdir);
+	gpdir |= gpio_mask(gpio);
+	out_be32(&base->gpdat, gpdir & data->dat_shadow);
+	out_be32(&base->gpdir, gpdir);
+
 	return 0;
 }
 
Signature · 1 line
2.23.0
#4 lore raw

PATCH RESEND 2/2 gpio: mpc8xxx: don't do RMW on gpdat register when setting value

Quoted 12 lines · Rasmus Villemoes

The driver correctly handles reading back the value of an output gpio by reading from the shadow register for output, and from gpdat for inputs.

Unfortunately, when setting the value of some gpio, we do a RMW cycle on the gpdat register without taking the shadow register into account, thus accidentally setting other output gpios (at least those whose value cannot be read back) to 0 at the same time.

When changing a gpio from input to output, we still need to make sure it initially has the requested value. So, the procedure is

Quoted 6 lines

- update the shadow register
- compute the new gpdir register
- write the bitwise and of the shadow and new gpdir register to gpdat
- write the new gpdir register

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200331/0887c3e8/attachment.sig>
#5 lore raw

cover gpio: mpc8xxx: honour shadow register when writing gpdat

In reply to Rasmus Villemoes
Quoted 6 lines · Rasmus Villemoes

Rasmus Villemoes (2):
gpio: mpc8xxx: don't modify gpdat when setting gpio as input
gpio: mpc8xxx: don't do RMW on gpdat register when setting value

 drivers/gpio/mpc8xxx_gpio.c | 41 ++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 26 deletions(-)

ping

#6 lore raw

cover gpio: mpc8xxx: honour shadow register when writing gpdat

In reply to Rasmus Villemoes
Quoted 6 lines · Rasmus Villemoes

[resending with Mario's correct address, sorry for the double post]

The driver correctly uses the shadow register when asked for the current value of an output gpio. Unfortunately, it does RMW on the gpdat register both when setting a gpio as input and output. These two patches fix that.

Ping. Any chance these fixes can make it to 2020.04 ?

Rasmus

#7 lore raw

cover gpio: mpc8xxx: honour shadow register when writing gpdat

Quoted 9 lines · Rasmus Villemoes
Quoted 6 lines · Rasmus Villemoes

[resending with Mario's correct address, sorry for the double post]

The driver correctly uses the shadow register when asked for the current value of an output gpio. Unfortunately, it does RMW on the gpdat register both when setting a gpio as input and output. These two patches fix that.

Ping. Any chance these fixes can make it to 2020.04 ?

Ping^3.

Tom, will you consider applying these directly? It's been two months and I haven't got any response. Or advise on what I could do.

Thanks,
Rasmus

#8 lore raw

cover gpio: mpc8xxx: honour shadow register when writing gpdat

Quoted 9 lines · Rasmus Villemoes
Quoted 8 lines · Rasmus Villemoes
Quoted 6 lines · Rasmus Villemoes

[resending with Mario's correct address, sorry for the double post]

The driver correctly uses the shadow register when asked for the current value of an output gpio. Unfortunately, it does RMW on the gpdat register both when setting a gpio as input and output. These two patches fix that.

Quoted 6 lines
Quoted 1 line

Ping. Any chance these fixes can make it to 2020.04 ?

Ping^3.

Tom, will you consider applying these directly? It's been two months and I haven't got any response. Or advise on what I could do.

I suppose that if Mario doesn't speak up soon I'll take a look at picking them up myself, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200323/9a10aa63/attachment.sig>
#9 lore raw

cover gpio: mpc8xxx: honour shadow register when writing gpdat

Quoted 12 lines · Tom Rini
Quoted 11 lines · Rasmus Villemoes
Quoted 9 lines · Rasmus Villemoes
Quoted 6 lines · Rasmus Villemoes

[resending with Mario's correct address, sorry for the double post]

The driver correctly uses the shadow register when asked for the current value of an output gpio. Unfortunately, it does RMW on the gpdat register both when setting a gpio as input and output. These two patches fix that.

Ping. Any chance these fixes can make it to 2020.04 ?

Quoted 8 lines
Quoted 4 lines

Ping^3.

Tom, will you consider applying these directly? It's been two months and I haven't got any response. Or advise on what I could do.

I suppose that if Mario doesn't speak up soon I'll take a look at picking them up myself, thanks!

Thanks. And in the unlikely event you have plenty of time, can I also have you look at the

https://patchwork.ozlabs.org/project/uboot/list/?series=157899

series? The mpc8xxx_spi driver is mostly unusable without the 4/5 patch, at least for accessing flash devices - perhaps some other spi peripherals never encounter the "transfer size is 3 mod 4".

Thanks,
Rasmus