Skip to contentSkip to conversation

lib: utils:/gpio: Improve the sifive gpio driver

PATCH Superseded Xiang W 4 participants 6 messages Oct 25, 2021 – Oct 26, 2021 SiFive drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH 1/1 lib: utils:/gpio: Improve the sifive gpio driver

1. Add input support
2. Add pull-up support
3. Add ACTIVE_LOW support

---
 lib/utils/gpio/fdt_gpio_sifive.c | 55 ++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
lib/utils/gpio/fdt_gpio_sifive.c +55 -0
index 677f0fa..d113195 100644
@@ -18,8 +18,12 @@
 #define SIFIVE_GPIO_PINS_MAX	32
 #define SIFIVE_GPIO_PINS_DEF	16
 
+#define SIFIVE_GPIO_INPUTVAL	0x00
+#define SIFIVE_GPIO_INPUTEN	0x04
 #define SIFIVE_GPIO_OUTEN	0x8
 #define SIFIVE_GPIO_OUTVAL	0xc
+#define SIFIVE_GPIO_PUE		0x10
+#define SIFIVE_GPIO_OUTXOR	0x40
 #define SIFIVE_GPIO_BIT(b)	(1U << (b))
 
 struct sifive_gpio_chip {
@@ -40,6 +44,20 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
 	v |= SIFIVE_GPIO_BIT(gp->offset);
 	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
 
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+	if (gp->flags & GPIO_FLAG_ACTIVE_LOW)
+		v |= SIFIVE_GPIO_BIT(gp->offset);
+	else
+		v &= ~SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+	if (gp->flags & GPIO_FLAG_PULL_UP)
+		v |= SIFIVE_GPIO_BIT(gp->offset);
+	else
+		v &= ~SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
 	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
 	if (!value)
 		v &= ~SIFIVE_GPIO_BIT(gp->offset);
@@ -50,6 +68,30 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
 	return 0;
 }
 
+static int sifive_gpio_direction_input(struct gpio_pin *gp)
+{
+	unsigned int v;
+	struct sifive_gpio_chip *chip =
+		container_of(gp->chip, struct sifive_gpio_chip, chip);
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+	v &= ~SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+	v |= SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+	if (gp->flags & GPIO_FLAG_PULL_UP)
+		v |= SIFIVE_GPIO_BIT(gp->offset);
+	else
+		v &= ~SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
+	return 0;
+}
+
 static void sifive_gpio_set(struct gpio_pin *gp, int value)
 {
 	unsigned int v;
@@ -64,6 +106,17 @@ static void sifive_gpio_set(struct gpio_pin *gp, int value)
 	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
 }
 
+static int sifive_gpio_get(struct gpio_pin *gp)
+{
+	unsigned int v, invert;
+	struct sifive_gpio_chip *chip =
+		container_of(gp->chip, struct sifive_gpio_chip, chip);
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTVAL));
+	v = !!(v & SIFIVE_GPIO_BIT(gp->offset));
+	invert = !!(gp->flags & GPIO_FLAG_ACTIVE_LOW);
+	return v ^ invert;
+}
+
 extern struct fdt_gpio fdt_gpio_sifive;
 
 static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
@@ -86,7 +139,9 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
 	chip->chip.id = phandle;
 	chip->chip.ngpio = SIFIVE_GPIO_PINS_DEF;
 	chip->chip.direction_output = sifive_gpio_direction_output;
+	chip->chip.direction_input = sifive_gpio_direction_input;
 	chip->chip.set = sifive_gpio_set;
+	chip->chip.get = sifive_gpio_get;
 	rc = gpio_chip_add(&chip->chip);
 	if (rc)
 		return rc;
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/1 lib: utils:/gpio: Improve the sifive gpio driver

Quoted 15 lines · Xiang W

1. Add input support
2. Add pull-up support
3. Add ACTIVE_LOW support

---
lib/utils/gpio/fdt_gpio_sifive.c | 55 ++++++++++++++++++++++++++++++++

1 file changed, 55 insertions(+)
lib/utils/gpio/fdt_gpio_sifive.c +0 -0
index 677f0fa..d113195 100644

lib/utils/gpio/fdt_gpio_sifive.c line 22

Quoted 5 lines
lib/utils/gpio/fdt_gpio_sifive.c +2 -0
@@ -18,8 +18,12 @@
 #define SIFIVE_GPIO_PINS_MAX	32
 #define SIFIVE_GPIO_PINS_DEF	16
 
+#define SIFIVE_GPIO_INPUTVAL	0x00
+#define SIFIVE_GPIO_INPUTEN	0x04

For consistency with OUT* these should be IN*.

lib/utils/gpio/fdt_gpio_sifive.c line 25

Quoted 3 lines
lib/utils/gpio/fdt_gpio_sifive.c +1 -0
 #define SIFIVE_GPIO_OUTEN	0x8
 #define SIFIVE_GPIO_OUTVAL	0xc
+#define SIFIVE_GPIO_PUE		0x10

Even Linux doesn?t bother with this.

Quoted 10 lines
lib/utils/gpio/fdt_gpio_sifive.c +3 -0
+#define SIFIVE_GPIO_OUTXOR	0x40
 #define SIFIVE_GPIO_BIT(b)	(1U << (b))
 
 struct sifive_gpio_chip {
@@ -40,6 +44,20 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
 	v |= SIFIVE_GPIO_BIT(gp->offset);
 	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
 
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+	if (gp->flags & GPIO_FLAG_ACTIVE_LOW)

lib/utils/gpio/fdt_gpio_sifive.c static int sifive_gpio_direction_output(struct gpio_pin *gp, int value) · line 58

Quoted 10 lines
lib/utils/gpio/fdt_gpio_sifive.c +10 -0
+		v |= SIFIVE_GPIO_BIT(gp->offset);
+	else
+		v &= ~SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+	if (gp->flags & GPIO_FLAG_PULL_UP)
+		v |= SIFIVE_GPIO_BIT(gp->offset);
+	else
+		v &= ~SIFIVE_GPIO_BIT(gp->offset);

Pull-up on an output sounds strange, that?d tie it to 1, no? Unless you have open-drain-like things, but that?s not the case here.

Quoted 38 lines
lib/utils/gpio/fdt_gpio_sifive.c +26 -0
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
 	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
 	if (!value)
 		v &= ~SIFIVE_GPIO_BIT(gp->offset);
@@ -50,6 +68,30 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
 	return 0;
 }
 
+static int sifive_gpio_direction_input(struct gpio_pin *gp)
+{
+	unsigned int v;
+	struct sifive_gpio_chip *chip =
+		container_of(gp->chip, struct sifive_gpio_chip, chip);
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+	v &= ~SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+	v |= SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+	if (gp->flags & GPIO_FLAG_PULL_UP)
+		v |= SIFIVE_GPIO_BIT(gp->offset);
+	else
+		v &= ~SIFIVE_GPIO_BIT(gp->offset);
+	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
+	return 0;
+}
+
 static void sifive_gpio_set(struct gpio_pin *gp, int value)
 {
 	unsigned int v;
@@ -64,6 +106,17 @@ static void sifive_gpio_set(struct gpio_pin *gp, int value)
 	writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));

lib/utils/gpio/fdt_gpio_sifive.c static void sifive_gpio_set(struct gpio_pin *gp, int value) · line 116

Quoted 10 lines
lib/utils/gpio/fdt_gpio_sifive.c +8 -0
 }
 
+static int sifive_gpio_get(struct gpio_pin *gp)
+{
+	unsigned int v, invert;
+	struct sifive_gpio_chip *chip =
+		container_of(gp->chip, struct sifive_gpio_chip, chip);
+	v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTVAL));
+	v = !!(v & SIFIVE_GPIO_BIT(gp->offset));
+	invert = !!(gp->flags & GPIO_FLAG_ACTIVE_LOW);

Can we not just use a normal != 0? !! always looks silly and confusing.

lib/utils/gpio/fdt_gpio_sifive.c static void sifive_gpio_set(struct gpio_pin *gp, int value) · line 117

Quoted 1 line
lib/utils/gpio/fdt_gpio_sifive.c +1 -0
+	return v ^ invert;

In Linux the inversion is done in generic code rather than repeating in every driver. This means you avoid a bunch of copy-pasting, but comes with the downside that you can?t take advantage of configurable hardware polarity. A conscious choice should be made about which route OpenSBI should go down, but I would personally favour the simplicity of doing it in generic code (shaving a couple of instructions off a GPIO read hardly matters).

Jess

Quoted 23 lines
lib/utils/gpio/fdt_gpio_sifive.c +4 -0
+}
+
 extern struct fdt_gpio fdt_gpio_sifive;
 
 static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
@@ -86,7 +139,9 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
 	chip->chip.id = phandle;
 	chip->chip.ngpio = SIFIVE_GPIO_PINS_DEF;
 	chip->chip.direction_output = sifive_gpio_direction_output;
+	chip->chip.direction_input = sifive_gpio_direction_input;
 	chip->chip.set = sifive_gpio_set;
+	chip->chip.get = sifive_gpio_get;
 	rc = gpio_chip_add(&chip->chip);
 	if (rc)
 		return rc;
Signature · 7 lines
2.30.2


-- 
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw

PATCH 1/1 lib: utils:/gpio: Improve the sifive gpio driver

In reply to Xiang W

+Green Wan +Vincent Chen

Quoted 6 lines · Xiang W

1. Add input support
2. Add pull-up support
3. Add ACTIVE_LOW support

@Green/Vincent, it would be great if you can review this patch ?

Regards,
Anup

Quoted 109 lines
---
 lib/utils/gpio/fdt_gpio_sifive.c | 55 ++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
lib/utils/gpio/fdt_gpio_sifive.c +55 -1
index 677f0fa..d113195 100644
@@ -18,8 +18,12 @@
 #define SIFIVE_GPIO_PINS_MAX   32
 #define SIFIVE_GPIO_PINS_DEF   16
 
+#define SIFIVE_GPIO_INPUTVAL   0x00
+#define SIFIVE_GPIO_INPUTEN    0x04
 #define SIFIVE_GPIO_OUTEN      0x8
 #define SIFIVE_GPIO_OUTVAL     0xc
+#define SIFIVE_GPIO_PUE                0x10
+#define SIFIVE_GPIO_OUTXOR     0x40
 #define SIFIVE_GPIO_BIT(b)     (1U << (b))
 
 struct sifive_gpio_chip {
@@ -40,6 +44,20 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
        v |= SIFIVE_GPIO_BIT(gp->offset);
        writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
 
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+       if (gp->flags & GPIO_FLAG_ACTIVE_LOW)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+       if (gp->flags & GPIO_FLAG_PULL_UP)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
        v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
        if (!value)
                v &= ~SIFIVE_GPIO_BIT(gp->offset);
@@ -50,6 +68,30 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
        return 0;
 }
 
+static int sifive_gpio_direction_input(struct gpio_pin *gp)
+{
+       unsigned int v;
+       struct sifive_gpio_chip *chip =
+               container_of(gp->chip, struct sifive_gpio_chip, chip);
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+       v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+       v |= SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+       if (gp->flags & GPIO_FLAG_PULL_UP)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
+       return 0;
+}
+
 static void sifive_gpio_set(struct gpio_pin *gp, int value)
 {
        unsigned int v;
@@ -64,6 +106,17 @@ static void sifive_gpio_set(struct gpio_pin *gp, int value)
        writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
 }
 
+static int sifive_gpio_get(struct gpio_pin *gp)
+{
+       unsigned int v, invert;
+       struct sifive_gpio_chip *chip =
+               container_of(gp->chip, struct sifive_gpio_chip, chip);
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTVAL));
+       v = !!(v & SIFIVE_GPIO_BIT(gp->offset));
+       invert = !!(gp->flags & GPIO_FLAG_ACTIVE_LOW);
+       return v ^ invert;
+}
+
 extern struct fdt_gpio fdt_gpio_sifive;
 
 static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
@@ -86,7 +139,9 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
        chip->chip.id = phandle;
        chip->chip.ngpio = SIFIVE_GPIO_PINS_DEF;
        chip->chip.direction_output = sifive_gpio_direction_output;
+       chip->chip.direction_input = sifive_gpio_direction_input;
        chip->chip.set = sifive_gpio_set;
+       chip->chip.get = sifive_gpio_get;
        rc = gpio_chip_add(&chip->chip);
        if (rc)
                return rc;
--

2.30.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#3 lore raw

PATCH 1/1 lib: utils:/gpio: Improve the sifive gpio driver

Oops, forgot to CC. Sorry for the noise.

Quoted 125 lines · Anup Patel

+Green Wan +Vincent Chen

Quoted 6 lines · Xiang W

1. Add input support
2. Add pull-up support
3. Add ACTIVE_LOW support

@Green/Vincent, it would be great if you can review this patch ?

Regards,
Anup

Quoted 109 lines
---
 lib/utils/gpio/fdt_gpio_sifive.c | 55 ++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
lib/utils/gpio/fdt_gpio_sifive.c +55 -1
index 677f0fa..d113195 100644
@@ -18,8 +18,12 @@
 #define SIFIVE_GPIO_PINS_MAX   32
 #define SIFIVE_GPIO_PINS_DEF   16
 
+#define SIFIVE_GPIO_INPUTVAL   0x00
+#define SIFIVE_GPIO_INPUTEN    0x04
 #define SIFIVE_GPIO_OUTEN      0x8
 #define SIFIVE_GPIO_OUTVAL     0xc
+#define SIFIVE_GPIO_PUE                0x10
+#define SIFIVE_GPIO_OUTXOR     0x40
 #define SIFIVE_GPIO_BIT(b)     (1U << (b))
 
 struct sifive_gpio_chip {
@@ -40,6 +44,20 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
        v |= SIFIVE_GPIO_BIT(gp->offset);
        writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
 
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+       if (gp->flags & GPIO_FLAG_ACTIVE_LOW)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+       if (gp->flags & GPIO_FLAG_PULL_UP)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
        v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
        if (!value)
                v &= ~SIFIVE_GPIO_BIT(gp->offset);
@@ -50,6 +68,30 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
        return 0;
 }
 
+static int sifive_gpio_direction_input(struct gpio_pin *gp)
+{
+       unsigned int v;
+       struct sifive_gpio_chip *chip =
+               container_of(gp->chip, struct sifive_gpio_chip, chip);
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+       v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+       v |= SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+       if (gp->flags & GPIO_FLAG_PULL_UP)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
+       return 0;
+}
+
 static void sifive_gpio_set(struct gpio_pin *gp, int value)
 {
        unsigned int v;
@@ -64,6 +106,17 @@ static void sifive_gpio_set(struct gpio_pin *gp, int value)
        writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
 }
 
+static int sifive_gpio_get(struct gpio_pin *gp)
+{
+       unsigned int v, invert;
+       struct sifive_gpio_chip *chip =
+               container_of(gp->chip, struct sifive_gpio_chip, chip);
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTVAL));
+       v = !!(v & SIFIVE_GPIO_BIT(gp->offset));
+       invert = !!(gp->flags & GPIO_FLAG_ACTIVE_LOW);
+       return v ^ invert;
+}
+
 extern struct fdt_gpio fdt_gpio_sifive;
 
 static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
@@ -86,7 +139,9 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
        chip->chip.id = phandle;
        chip->chip.ngpio = SIFIVE_GPIO_PINS_DEF;
        chip->chip.direction_output = sifive_gpio_direction_output;
+       chip->chip.direction_input = sifive_gpio_direction_input;
        chip->chip.set = sifive_gpio_set;
+       chip->chip.get = sifive_gpio_get;
        rc = gpio_chip_add(&chip->chip);
        if (rc)
                return rc;
--

2.30.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#4 lore raw

PATCH 1/1 lib: utils:/gpio: Improve the sifive gpio driver

+ Andy (he is reviewing the patch and verifying the patch afterward)

Sorry for replying late. I just found the email was blocked and never being sent yesterday.

Hi Xiang,

Could you share how you verify the patch? QEMU?

Hi Anup,

I checked the v2 patch below. It looks good to me.
http://lists.infradead.org/pipermail/opensbi/2021-October/001934.html

As for the active_low logic, should we put more description to include/sbi_utils/gpio/gpio.h? Explicitly define the function set the GPIO pin value with/without ACTIVE_LOW status. something like below:

- * Get current value of GPIO pin
+ * Get current value of GPIO pin. Take its ACTIVE_LOW status
into account. */

Quoted 129 lines · Anup Patel

Oops, forgot to CC. Sorry for the noise.

Quoted 125 lines · Anup Patel

+Green Wan +Vincent Chen

Quoted 6 lines · Xiang W

1. Add input support
2. Add pull-up support
3. Add ACTIVE_LOW support

@Green/Vincent, it would be great if you can review this patch ?

Regards,
Anup

Quoted 109 lines
---
 lib/utils/gpio/fdt_gpio_sifive.c | 55 ++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
lib/utils/gpio/fdt_gpio_sifive.c +55 -1
index 677f0fa..d113195 100644
@@ -18,8 +18,12 @@
 #define SIFIVE_GPIO_PINS_MAX   32
 #define SIFIVE_GPIO_PINS_DEF   16
 
+#define SIFIVE_GPIO_INPUTVAL   0x00
+#define SIFIVE_GPIO_INPUTEN    0x04
 #define SIFIVE_GPIO_OUTEN      0x8
 #define SIFIVE_GPIO_OUTVAL     0xc
+#define SIFIVE_GPIO_PUE                0x10
+#define SIFIVE_GPIO_OUTXOR     0x40
 #define SIFIVE_GPIO_BIT(b)     (1U << (b))
 
 struct sifive_gpio_chip {
@@ -40,6 +44,20 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
        v |= SIFIVE_GPIO_BIT(gp->offset);
        writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
 
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+       if (gp->flags & GPIO_FLAG_ACTIVE_LOW)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+       if (gp->flags & GPIO_FLAG_PULL_UP)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
        v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
        if (!value)
                v &= ~SIFIVE_GPIO_BIT(gp->offset);
@@ -50,6 +68,30 @@ static int sifive_gpio_direction_output(struct gpio_pin *gp, int value)
        return 0;
 }
 
+static int sifive_gpio_direction_input(struct gpio_pin *gp)
+{
+       unsigned int v;
+       struct sifive_gpio_chip *chip =
+               container_of(gp->chip, struct sifive_gpio_chip, chip);
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+       v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTEN));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+       v |= SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+       if (gp->flags & GPIO_FLAG_PULL_UP)
+               v |= SIFIVE_GPIO_BIT(gp->offset);
+       else
+               v &= ~SIFIVE_GPIO_BIT(gp->offset);
+       writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
+       return 0;
+}
+
 static void sifive_gpio_set(struct gpio_pin *gp, int value)
 {
        unsigned int v;
@@ -64,6 +106,17 @@ static void sifive_gpio_set(struct gpio_pin *gp, int value)
        writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
 }
 
+static int sifive_gpio_get(struct gpio_pin *gp)
+{
+       unsigned int v, invert;
+       struct sifive_gpio_chip *chip =
+               container_of(gp->chip, struct sifive_gpio_chip, chip);
+       v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTVAL));
+       v = !!(v & SIFIVE_GPIO_BIT(gp->offset));
+       invert = !!(gp->flags & GPIO_FLAG_ACTIVE_LOW);
+       return v ^ invert;
+}
+
 extern struct fdt_gpio fdt_gpio_sifive;
 
 static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
@@ -86,7 +139,9 @@ static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,
        chip->chip.id = phandle;
        chip->chip.ngpio = SIFIVE_GPIO_PINS_DEF;
        chip->chip.direction_output = sifive_gpio_direction_output;
+       chip->chip.direction_input = sifive_gpio_direction_input;
        chip->chip.set = sifive_gpio_set;
+       chip->chip.get = sifive_gpio_get;
        rc = gpio_chip_add(&chip->chip);
        if (rc)
                return rc;
--

2.30.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#5 lore raw

PATCH 1/1 lib: utils:/gpio: Improve the sifive gpio driver

? 2021-10-26???? 10:33 +0800?Green Wan???

Quoted 8 lines

+ Andy (he is reviewing the patch and verifying the patch afterward)

Sorry for replying late. I just found the email was blocked and never being sent yesterday.

Hi Xiang,

Could you share how you verify the patch? QEMU?

The changes I made through the datasheet without tested.

Regards,
Xiang W

Quoted 172 lines

Hi Anup,

I checked the v2 patch below. It looks good to me.
http://lists.infradead.org/pipermail/opensbi/2021-October/001934.html

As for the active_low logic, should we put more description to include/sbi_utils/gpio/gpio.h? Explicitly define the function set the GPIO pin value with/without ACTIVE_LOW status. something like below:

-??????? * Get current value of GPIO pin
+??????? * Get current value of GPIO pin. Take its ACTIVE_LOW status into account. */

Quoted 155 lines · Anup Patel

Oops, forgot to CC. Sorry for the noise.

Quoted 150 lines · Anup Patel

+Green Wan? +Vincent Chen

Quoted 6 lines · Xiang W

1. Add input support
2. Add pull-up support
3. Add ACTIVE_LOW support

@Green/Vincent, it would be great if you can review this patch ?

Regards,
Anup

Quoted 134 lines

---
?lib/utils/gpio/fdt_gpio_sifive.c | 55
++++++++++++++++++++++++++++++++
?1 file changed, 55 insertions(+)

lib/utils/gpio/fdt_gpio_sifive.c +0 -0
index 677f0fa..d113195 100644
@@ -18,8 +18,12 @@

?#define SIFIVE_GPIO_PINS_MAX?? 32
?#define SIFIVE_GPIO_PINS_DEF?? 16

lib/utils/gpio/fdt_gpio_sifive.c +2 -0
+#define SIFIVE_GPIO_INPUTVAL?? 0x00
+#define SIFIVE_GPIO_INPUTEN??? 0x04

?#define SIFIVE_GPIO_OUTEN????? 0x8
?#define SIFIVE_GPIO_OUTVAL???? 0xc

lib/utils/gpio/fdt_gpio_sifive.c +2 -0
+#define SIFIVE_GPIO_PUE??????????????? 0x10
+#define SIFIVE_GPIO_OUTXOR???? 0x40

?#define SIFIVE_GPIO_BIT(b)???? (1U << (b))

?struct sifive_gpio_chip {

lib/utils/gpio/fdt_gpio_sifive.c +0 -0
@@ -40,6 +44,20 @@ static int
 sifive_gpio_direction_output(struct gpio_pin *gp, int value)
 ??????? v |= SIFIVE_GPIO_BIT(gp->offset);

??????? writel(v, (volatile void *)(chip->addr +
SIFIVE_GPIO_OUTEN));

+?????? v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+?????? if (gp->flags & GPIO_FLAG_ACTIVE_LOW) +?????????????? v |= SIFIVE_GPIO_BIT(gp->offset); +?????? else
+?????????????? v &= ~SIFIVE_GPIO_BIT(gp->offset); +?????? writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_OUTXOR));
+
+?????? v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+?????? if (gp->flags & GPIO_FLAG_PULL_UP) +?????????????? v |= SIFIVE_GPIO_BIT(gp->offset); +?????? else
+?????????????? v &= ~SIFIVE_GPIO_BIT(gp->offset); +?????? writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+
??????? v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_OUTVAL));
??????? if (!value)
??????????????? v &= ~SIFIVE_GPIO_BIT(gp->offset);

lib/utils/gpio/fdt_gpio_sifive.c +9 -0
@@ -50,6 +68,30 @@ static int
 sifive_gpio_direction_output(struct gpio_pin *gp, int value)
 ??????? return 0;
 ?}
 
+static int sifive_gpio_direction_input(struct gpio_pin *gp)
+{
+?????? unsigned int v;
+?????? struct sifive_gpio_chip *chip =
+?????????????? container_of(gp->chip, struct sifive_gpio_chip,
 chip);
+
+?????? v = readl((volatile void *)(chip->addr +
 SIFIVE_GPIO_OUTEN));
+?????? v &= ~SIFIVE_GPIO_BIT(gp->offset);
+?????? writel(v, (volatile void *)(chip->addr +

SIFIVE_GPIO_OUTEN));
+
+?????? v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+?????? v |= SIFIVE_GPIO_BIT(gp->offset); +?????? writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_INPUTEN));
+
+?????? v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_PUE));
+?????? if (gp->flags & GPIO_FLAG_PULL_UP) +?????????????? v |= SIFIVE_GPIO_BIT(gp->offset); +?????? else
+?????????????? v &= ~SIFIVE_GPIO_BIT(gp->offset); +?????? writel(v, (volatile void *)(chip->addr + SIFIVE_GPIO_PUE));

lib/utils/gpio/fdt_gpio_sifive.c +4 -0
+
+?????? return 0;
+}
+

?static void sifive_gpio_set(struct gpio_pin *gp, int value) ?{
??????? unsigned int v;

lib/utils/gpio/fdt_gpio_sifive.c +0 -0
@@ -64,6 +106,17 @@ static void sifive_gpio_set(struct gpio_pin
 *gp, int value)

??????? writel(v, (volatile void *)(chip->addr +
SIFIVE_GPIO_OUTVAL));
?}

lib/utils/gpio/fdt_gpio_sifive.c +5 -0
+static int sifive_gpio_get(struct gpio_pin *gp)
+{
+?????? unsigned int v, invert;
+?????? struct sifive_gpio_chip *chip =
+?????????????? container_of(gp->chip, struct sifive_gpio_chip,

chip);
+?????? v = readl((volatile void *)(chip->addr + SIFIVE_GPIO_INPUTVAL));
+?????? v = !!(v & SIFIVE_GPIO_BIT(gp->offset)); +?????? invert = !!(gp->flags & GPIO_FLAG_ACTIVE_LOW); +?????? return v ^ invert;

lib/utils/gpio/fdt_gpio_sifive.c +2 -0
+}
+

?extern struct fdt_gpio fdt_gpio_sifive;

?static int sifive_gpio_init(void *fdt, int nodeoff, u32 phandle,

lib/utils/gpio/fdt_gpio_sifive.c +0 -0
@@ -86,7 +139,9 @@ static int sifive_gpio_init(void *fdt, int
 nodeoff, u32 phandle,
 ??????? chip->chip.id = phandle;
 ??????? chip->chip.ngpio = SIFIVE_GPIO_PINS_DEF;

??????? chip->chip.direction_output =
sifive_gpio_direction_output;
+?????? chip->chip.direction_input =
sifive_gpio_direction_input;
??????? chip->chip.set = sifive_gpio_set;
+?????? chip->chip.get = sifive_gpio_get;
??????? rc = gpio_chip_add(&chip->chip);
??????? if (rc)
??????????????? return rc;
--
2.30.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi