Skip to contentSkip to conversation

gpio: Fix gpio-generic bug and add driver for GRGPIO cores

PATCH In review Andreas Larsson 2 participants 8 messages Feb 5, 2013 – Mar 2, 2013 GPIO

lore
Share & tools
lore
#0 lore raw

cover gpio: Fix gpio-generic bug and add driver for GRGPIO cores

The second patch is v2 of the earlier single GRGPIO patch

Andreas Larsson (2):
gpio: gpio-generic: Fix bug in big endian bit conversion
gpio: Add device driver for GRGPIO cores

 drivers/gpio/Kconfig        |    8 ++
 drivers/gpio/Makefile       |    1 +
 drivers/gpio/gpio-generic.c |    5 +-
 drivers/gpio/gpio-grgpio.c  |  244 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 257 insertions(+), 1 deletions(-)
 create mode 100644 drivers/gpio/gpio-grgpio.c
#1 lore raw

PATCH 1/2 gpio: gpio-generic: Fix bug in big endian bit conversion

The swap to convert LE to BE in bgpio_pin2mask_be should be on byte level, not on bit level.

---
 drivers/gpio/gpio-generic.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
drivers/gpio/gpio-generic.c +4 -1
index 05fcc0f..7f11537 100644
@@ -112,7 +112,10 @@ static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
 static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
 				       unsigned int pin)
 {
-	return 1 << (bgc->bits - 1 - pin);
+	unsigned int bit = pin & 0x7; /* Bit number within byte */
+	unsigned int base = pin - bit; /* Pin that is bit 0 within byte */
+
+	return 1 << ((bgc->bits - base - 8) + bit); /* shifted base + bit */
 }
 
 static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [PATCH 1/2] gpio: gpio-generic: Fix bug in big endian bit conversion

Quoted 13 lines · Andreas Larsson

The swap to convert LE to BE in bgpio_pin2mask_be should be on byte level, not on bit level.

---
 drivers/gpio/gpio-generic.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
drivers/gpio/gpio-generic.c +0 -0
index 05fcc0f..7f11537 100644

drivers/gpio/gpio-generic.c static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin) · line 118

Quoted 8 lines
drivers/gpio/gpio-generic.c +4 -1
@@ -112,7 +112,10 @@ static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
 static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
 				       unsigned int pin)
 {
-	return 1 << (bgc->bits - 1 - pin);
+	unsigned int bit = pin & 0x7; /* Bit number within byte */
+	unsigned int base = pin - bit; /* Pin that is bit 0 within byte */
+
+	return 1 << ((bgc->bits - base - 8) + bit); /* shifted base + bit */

Ah, sorry for my previous reply. I see you have seen gpio-generic. :-)

No, the original calculation is correct. BE and LE bit numbering are opposite, bit Linux always uses LE numbers as far as bit masks are concerned. Therefore pin 0 is the most significant bit, and pin (nr_bits-1) is the least significant bit.

g.

#3 lore raw

Re: [PATCH 1/2] gpio: gpio-generic: Fix bug in big endian bit conversion

Quoted 25 lines · Grant Likely
Quoted 13 lines · Andreas Larsson

The swap to convert LE to BE in bgpio_pin2mask_be should be on byte level, not on bit level.

---
 drivers/gpio/gpio-generic.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
drivers/gpio/gpio-generic.c +0 -0
index 05fcc0f..7f11537 100644

drivers/gpio/gpio-generic.c static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin) · line 118

Quoted 8 lines
drivers/gpio/gpio-generic.c +4 -1
@@ -112,7 +112,10 @@ static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
 static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
 				       unsigned int pin)
 {
-	return 1 << (bgc->bits - 1 - pin);
+	unsigned int bit = pin & 0x7; /* Bit number within byte */
+	unsigned int base = pin - bit; /* Pin that is bit 0 within byte */
+
+	return 1 << ((bgc->bits - base - 8) + bit); /* shifted base + bit */

Ah, sorry for my previous reply. I see you have seen gpio-generic. :-)

Quoted 4 lines

No, the original calculation is correct. BE and LE bit numbering are opposite, bit Linux always uses LE numbers as far as bit masks are concerned. Therefore pin 0 is the most significant bit, and pin (nr_bits-1) is the least significant bit.

Hi Andreas,

Actually I'm wrong here (at least partially) after looking closer at the generic gpio driver. The problem is that things get messed up on 16 or 32 bit access depending on how the hardware expects to be accessed. bgpio always uses iowrite/ioread for register access which is inherently little endian, but if the hardware is wired up as a big-endian device then you have to do the fiddly bit you did above to get the bit positions to line up where you what then. So, there could potentially be 4 different ways to count bits on a value ioread() from a gpio register:

little-endian, LSB = 0 (sane)
little-endian, MSB = 0 (odd)
big-endian (bytes swapped), MSB = 0 (common on BE platforms) big-endian (bytes swapped), LSB = 0 (why are you making my life hard?)

We /could/ have a ioread32be/write32be mode in the driver, but I don't think that is the right approach. It means we need yet another set of accessors for register except using the 'be' variants. Blech. What I'd actually like to do is add a bitmask field to the gpio_desc which can be calculated ahead of time to whatever madness is required from the way the device is wired. Then the access routines don't need to even care. they just apply the bitmask to ioread/iowrite and it doesn't even need to know what the bit number actually is. The new support for gpio_desc in the core code makes this feasable.

g.

#4 lore raw

Re: [PATCH 1/2] gpio: gpio-generic: Fix bug in big endian bit conversion

Quoted 42 lines · Grant Likely
Quoted 25 lines · Grant Likely
Quoted 13 lines · Andreas Larsson

The swap to convert LE to BE in bgpio_pin2mask_be should be on byte level, not on bit level.

---
  drivers/gpio/gpio-generic.c |    5 ++++-
  1 files changed, 4 insertions(+), 1 deletions(-)
drivers/gpio/gpio-generic.c +0 -0
index 05fcc0f..7f11537 100644

drivers/gpio/gpio-generic.c static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin) · line 118

Quoted 8 lines
drivers/gpio/gpio-generic.c +4 -1
@@ -112,7 +112,10 @@ static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
  static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
  				       unsigned int pin)
  {
-	return 1 << (bgc->bits - 1 - pin);
+	unsigned int bit = pin & 0x7; /* Bit number within byte */
+	unsigned int base = pin - bit; /* Pin that is bit 0 within byte */
+
+	return 1 << ((bgc->bits - base - 8) + bit); /* shifted base + bit */

Ah, sorry for my previous reply. I see you have seen gpio-generic. :-)

Quoted 4 lines

No, the original calculation is correct. BE and LE bit numbering are opposite, bit Linux always uses LE numbers as far as bit masks are concerned. Therefore pin 0 is the most significant bit, and pin (nr_bits-1) is the least significant bit.

Hi Andreas,

Actually I'm wrong here (at least partially) after looking closer at the generic gpio driver. The problem is that things get messed up on 16 or 32 bit access depending on how the hardware expects to be accessed. bgpio always uses iowrite/ioread for register access which is inherently little endian, but if the hardware is wired up as a big-endian device then you have to do the fiddly bit you did above to get the bit positions to line up where you what then. So, there could potentially be 4 different ways to count bits on a value ioread() from a gpio register:

Quoted 4 lines

little-endian, LSB = 0 (sane)
little-endian, MSB = 0 (odd)
big-endian (bytes swapped), MSB = 0 (common on BE platforms) big-endian (bytes swapped), LSB = 0 (why are you making my life hard?)

Yes, in v4 of my patch I solved it using custom accessors set outside of gpio-generic internally using ioread32be/iowrite32be instead of adding a whole set of be variants in gpio-generic.

Quoted 9 lines

We /could/ have a ioread32be/write32be mode in the driver, but I don't think that is the right approach. It means we need yet another set of accessors for register except using the 'be' variants. Blech. What I'd actually like to do is add a bitmask field to the gpio_desc which can be calculated ahead of time to whatever madness is required from the way the device is wired. Then the access routines don't need to even care. they just apply the bitmask to ioread/iowrite and it doesn't even need to know what the bit number actually is. The new support for gpio_desc in the core code makes this feasable.

I am not sure I understand what you mean here or what new support for gpio_desc you are referring to (looking in gpio/next at git://git.secretlab.ca/git/linux-2.6).

Do you mean to add something like an 'unsigned long bitmask[64]' bitmap array with one bitmap for each gpio line to struct gpio_desc and use this primarily by gpio-generic.c, populated in bgpio_init? Is gpio_desc now available outside of gpiolib.c in some repository/branch that I might be unaware of?

Cheers,
Andreas

#5 lore raw

Re: [PATCH 1/2] gpio: gpio-generic: Fix bug in big endian bit conversion

Quoted 17 lines · Andreas Larsson
Quoted 11 lines · Grant Likely

On Sat, 09 Feb 2013 14:58:55 +0000, Grant Likely <[email protected]> wrote:
We /could/ have a ioread32be/write32be mode in the driver, but I don't think that is the right approach. It means we need yet another set of accessors for register except using the 'be' variants. Blech. What I'd actually like to do is add a bitmask field to the gpio_desc which can be calculated ahead of time to whatever madness is required from the way the device is wired. Then the access routines don't need to even care. they just apply the bitmask to ioread/iowrite and it doesn't even need to know what the bit number actually is. The new support for gpio_desc in the core code makes this feasable.

I am not sure I understand what you mean here or what new support for gpio_desc you are referring to (looking in gpio/next at
git://git.secretlab.ca/git/linux-2.6).

Quoted 5 lines

Do you mean to add something like an 'unsigned long bitmask[64]' bitmap array with one bitmap for each gpio line to struct gpio_desc and use this primarily by gpio-generic.c, populated in bgpio_init? Is gpio_desc now available outside of gpiolib.c in some repository/branch that I might be unaware of?

Ah, I realize that it is the "gpiolib: remove gpio_desc[] static array" patch series you refer to, not yet pushed to gpio/next at git://git.secretlab.ca/git/linux-2.6.

The question on how you intend the bitmap field to be defined/used/populated remains.

Cheers,
Andreas

#6 lore raw

Re: [PATCH 1/2] gpio: gpio-generic: Fix bug in big endian bit conversion

In reply to Andreas Larsson
Quoted 43 lines · Andreas Larsson
Quoted 42 lines · Grant Likely
Quoted 25 lines · Grant Likely
Quoted 13 lines · Andreas Larsson

The swap to convert LE to BE in bgpio_pin2mask_be should be on byte level, not on bit level.

---
  drivers/gpio/gpio-generic.c |    5 ++++-
  1 files changed, 4 insertions(+), 1 deletions(-)
drivers/gpio/gpio-generic.c +0 -0
index 05fcc0f..7f11537 100644

drivers/gpio/gpio-generic.c static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin) · line 118

Quoted 8 lines
drivers/gpio/gpio-generic.c +4 -1
@@ -112,7 +112,10 @@ static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
  static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
  				       unsigned int pin)
  {
-	return 1 << (bgc->bits - 1 - pin);
+	unsigned int bit = pin & 0x7; /* Bit number within byte */
+	unsigned int base = pin - bit; /* Pin that is bit 0 within byte */
+
+	return 1 << ((bgc->bits - base - 8) + bit); /* shifted base + bit */

Ah, sorry for my previous reply. I see you have seen gpio-generic. :-)

Quoted 4 lines

No, the original calculation is correct. BE and LE bit numbering are opposite, bit Linux always uses LE numbers as far as bit masks are concerned. Therefore pin 0 is the most significant bit, and pin (nr_bits-1) is the least significant bit.

Hi Andreas,

Actually I'm wrong here (at least partially) after looking closer at the generic gpio driver. The problem is that things get messed up on 16 or 32 bit access depending on how the hardware expects to be accessed. bgpio always uses iowrite/ioread for register access which is inherently little endian, but if the hardware is wired up as a big-endian device then you have to do the fiddly bit you did above to get the bit positions to line up where you what then. So, there could potentially be 4 different ways to count bits on a value ioread() from a gpio register:

Quoted 8 lines
Quoted 4 lines

little-endian, LSB = 0 (sane)
little-endian, MSB = 0 (odd)
big-endian (bytes swapped), MSB = 0 (common on BE platforms) big-endian (bytes swapped), LSB = 0 (why are you making my life hard?)

Yes, in v4 of my patch I solved it using custom accessors set outside of gpio-generic internally using ioread32be/iowrite32be instead of adding a whole set of be variants in gpio-generic.

Until we've got something better, please do add the be accessor versions to gpio-generic. I'm not thrilled with the need for them, but it is still better than coding your own version.

Quoted 14 lines
Quoted 9 lines

We /could/ have a ioread32be/write32be mode in the driver, but I don't think that is the right approach. It means we need yet another set of accessors for register except using the 'be' variants. Blech. What I'd actually like to do is add a bitmask field to the gpio_desc which can be calculated ahead of time to whatever madness is required from the way the device is wired. Then the access routines don't need to even care. they just apply the bitmask to ioread/iowrite and it doesn't even need to know what the bit number actually is. The new support for gpio_desc in the core code makes this feasable.

I am not sure I understand what you mean here or what new support for gpio_desc you are referring to (looking in gpio/next at
git://git.secretlab.ca/git/linux-2.6).

Quoted 5 lines

Do you mean to add something like an 'unsigned long bitmask[64]' bitmap array with one bitmap for each gpio line to struct gpio_desc and use this primarily by gpio-generic.c, populated in bgpio_init? Is gpio_desc now available outside of gpiolib.c in some repository/branch that I might be unaware of?

No this isn't something that is anywhere, neither on the list nor in a branch. It's an idea that I've been toying around with along side pulling basic MMIO access directly into gpiolib to create a fast path. It's not even a fully thought-through idea. :-)

The idea is that each gpio_desc would get a hwbit field that would be pre-populated with the bit used when ioread/iowrite (non-be) accessors are used. If it is a BE register, then the bit positions can be munged at setup time so that the accessor itself doesn't have to muck around with bit position calcuation. This isn't significant for i2c or spi GPIO controllers because they are slow anyway, but for MMIO, it is a really big deal. It would mean dropping an entire callback call which is 10's of instructions compaired to the 2 or 3 required to write the bit to a set/clr register.

g.

#7 lore raw

PATCH 2/2 gpio: Add device driver for GRGPIO cores

In reply to Andreas Larsson

This driver supports GRGPIO gpio cores available in the GRLIB VHDL IP core library.

---
v2 of the eariler single GRGPIO patch

 drivers/gpio/Kconfig       |    8 ++
 drivers/gpio/Makefile      |    1 +
 drivers/gpio/gpio-grgpio.c |  244 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 253 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpio/gpio-grgpio.c
drivers/gpio/Kconfig +8 -0
index 682de75..8437ab7 100644
@@ -298,6 +298,14 @@ config GPIO_GE_FPGA
 	  and write pin state) for GPIO implemented in a number of GE single
 	  board computers.
 
+config GPIO_GRGPIO
+	tristate "Aeroflex Gaisler GRGPIO support"
+	depends on OF
+	select GPIO_GENERIC
+	help
+	  Select this to support Aeroflex Gaisler GRGPIO cores from the GRLIB
+	  VHDL IP core library.
+
 comment "I2C GPIO expanders:"
 
 config GPIO_ARIZONA
drivers/gpio/Makefile +1 -0
index c5aebd0..ca5d7a3 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_ARCH_DAVINCI)	+= gpio-davinci.o
 obj-$(CONFIG_GPIO_EM)		+= gpio-em.o
 obj-$(CONFIG_GPIO_EP93XX)	+= gpio-ep93xx.o
 obj-$(CONFIG_GPIO_GE_FPGA)	+= gpio-ge.o
+obj-$(CONFIG_GPIO_GRGPIO)	+= gpio-grgpio.o
 obj-$(CONFIG_GPIO_ICH)		+= gpio-ich.o
 obj-$(CONFIG_GPIO_IT8761E)	+= gpio-it8761e.o
 obj-$(CONFIG_GPIO_JANZ_TTL)	+= gpio-janz-ttl.o
drivers/gpio/gpio-grgpio.c new file +244 -0

244 lines not on this page · raw message

Signature · 1 line
1.7.0.4