Skip to contentSkip to conversation

lib: utils/reset: Register separate GPIO system reset devices

PATCH New Anup Patel 4 participants 5 messages Oct 21, 2021 – Nov 2, 2021

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 1/1 lib: utils/reset: Register separate GPIO system reset devices

Now that sbi_system support multiple system reset devices, we should register separate devices for GPIO restart and GPIO poweroff because DT describes these as separate devices with different compatible strings.

---
 lib/utils/reset/fdt_reset_gpio.c | 60 ++++++++++++++++++++++----------
 1 file changed, 41 insertions(+), 19 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +41 -19
index 4da1450..302035b 100644
@@ -35,20 +35,20 @@ static struct gpio_reset restart = {
 	.inactive_delay = 100
 };
 
-static struct gpio_reset *gpio_get_reset_settings(u32 type)
+static struct gpio_reset *gpio_reset_get(bool is_restart, u32 type)
 {
-	struct gpio_reset *reset;
+	struct gpio_reset *reset = NULL;
 
 	switch (type) {
 	case SBI_SRST_RESET_TYPE_SHUTDOWN:
-		reset = &poweroff;
+		if (!is_restart)
+			reset = &poweroff;
 		break;
 	case SBI_SRST_RESET_TYPE_COLD_REBOOT:
 	case SBI_SRST_RESET_TYPE_WARM_REBOOT:
-		reset = &restart;
+		if (is_restart)
+			reset = &restart;
 		break;
-	default:
-		reset = NULL;
 	}
 
 	if (reset && !reset->pin.chip)
@@ -57,15 +57,8 @@ static struct gpio_reset *gpio_get_reset_settings(u32 type)
 	return reset;
 }
 
-static int gpio_system_reset_check(u32 type, u32 reason)
+static void gpio_reset_exec(struct gpio_reset *reset)
 {
-	return !!gpio_get_reset_settings(type);
-}
-
-static void gpio_system_reset(u32 type, u32 reason)
-{
-	struct gpio_reset *reset = gpio_get_reset_settings(type);
-
 	if (reset) {
 		/* drive it active, also inactive->active edge */
 		gpio_direction_output(&reset->pin, 1);
@@ -82,10 +75,36 @@ static void gpio_system_reset(u32 type, u32 reason)
 	sbi_hart_hang();
 }
 
-static struct sbi_system_reset_device gpio_reset = {
-	.name = "gpio-reset",
-	.system_reset_check = gpio_system_reset_check,
-	.system_reset = gpio_system_reset
+static int gpio_system_poweroff_check(u32 type, u32 reason)
+{
+	return !!gpio_reset_get(FALSE, type);
+}
+
+static void gpio_system_poweroff(u32 type, u32 reason)
+{
+	gpio_reset_exec(gpio_reset_get(FALSE, type));
+}
+
+static struct sbi_system_reset_device gpio_poweroff = {
+	.name = "gpio-poweroff",
+	.system_reset_check = gpio_system_poweroff_check,
+	.system_reset = gpio_system_poweroff
+};
+
+static int gpio_system_restart_check(u32 type, u32 reason)
+{
+	return !!gpio_reset_get(TRUE, type);
+}
+
+static void gpio_system_restart(u32 type, u32 reason)
+{
+	gpio_reset_exec(gpio_reset_get(TRUE, type));
+}
+
+static struct sbi_system_reset_device gpio_restart = {
+	.name = "gpio-restart",
+	.system_reset_check = gpio_system_restart_check,
+	.system_reset = gpio_system_restart
 };
 
 static int gpio_reset_init(void *fdt, int nodeoff,
@@ -115,7 +134,10 @@ static int gpio_reset_init(void *fdt, int nodeoff,
 	if (len > 0)
 		reset->inactive_delay = fdt32_to_cpu(*val);
 
-	sbi_system_reset_add_device(&gpio_reset);
+	if (is_restart)
+		sbi_system_reset_add_device(&gpio_restart);
+	else
+		sbi_system_reset_add_device(&gpio_poweroff);
 
 	return 0;
 }
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 lib: utils/reset: Register separate GPIO system reset devices

Quoted 6 lines · anup patel anup.patel at wdc.com

Now that sbi_system support multiple system reset devices, we should register separate devices for GPIO restart and GPIO poweroff because DT describes these as separate devices with different compatible strings.

Looks good to me.

Quoted 113 lines

---
lib/utils/reset/fdt_reset_gpio.c | 60 ++++++++++++++++++++++----------

1 file changed, 41 insertions(+), 19 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +41 -20
index 4da1450..302035b 100644
@@ -35,20 +35,20 @@ static struct gpio_reset restart = {
 	.inactive_delay = 100
 };
 
-static struct gpio_reset *gpio_get_reset_settings(u32 type)
+static struct gpio_reset *gpio_reset_get(bool is_restart, u32 type)
 {
-	struct gpio_reset *reset;
+	struct gpio_reset *reset = NULL;
 
 	switch (type) {
 	case SBI_SRST_RESET_TYPE_SHUTDOWN:
-		reset = &poweroff;
+		if (!is_restart)
+			reset = &poweroff;
 		break;
 	case SBI_SRST_RESET_TYPE_COLD_REBOOT:
 	case SBI_SRST_RESET_TYPE_WARM_REBOOT:
-		reset = &restart;
+		if (is_restart)
+			reset = &restart;
 		break;
-	default:
-		reset = NULL;
 	}
 
 	if (reset && !reset->pin.chip)
@@ -57,15 +57,8 @@ static struct gpio_reset *gpio_get_reset_settings(u32 type)
 	return reset;
 }
 
-static int gpio_system_reset_check(u32 type, u32 reason)
+static void gpio_reset_exec(struct gpio_reset *reset)
 {
-	return !!gpio_get_reset_settings(type);
-}
-
-static void gpio_system_reset(u32 type, u32 reason)
-{
-	struct gpio_reset *reset = gpio_get_reset_settings(type);
-
 	if (reset) {
 		/* drive it active, also inactive->active edge */
 		gpio_direction_output(&reset->pin, 1);
@@ -82,10 +75,36 @@ static void gpio_system_reset(u32 type, u32 reason)
 	sbi_hart_hang();
 }
 
-static struct sbi_system_reset_device gpio_reset = {
-	.name = "gpio-reset",
-	.system_reset_check = gpio_system_reset_check,
-	.system_reset = gpio_system_reset
+static int gpio_system_poweroff_check(u32 type, u32 reason)
+{
+	return !!gpio_reset_get(FALSE, type);
+}
+
+static void gpio_system_poweroff(u32 type, u32 reason)
+{
+	gpio_reset_exec(gpio_reset_get(FALSE, type));
+}
+
+static struct sbi_system_reset_device gpio_poweroff = {
+	.name = "gpio-poweroff",
+	.system_reset_check = gpio_system_poweroff_check,
+	.system_reset = gpio_system_poweroff
+};
+
+static int gpio_system_restart_check(u32 type, u32 reason)
+{
+	return !!gpio_reset_get(TRUE, type);
+}
+
+static void gpio_system_restart(u32 type, u32 reason)
+{
+	gpio_reset_exec(gpio_reset_get(TRUE, type));
+}
+
+static struct sbi_system_reset_device gpio_restart = {
+	.name = "gpio-restart",
+	.system_reset_check = gpio_system_restart_check,
+	.system_reset = gpio_system_restart
 };
 
 static int gpio_reset_init(void *fdt, int nodeoff,
@@ -115,7 +134,10 @@ static int gpio_reset_init(void *fdt, int nodeoff,
 	if (len > 0)
 		reset->inactive_delay = fdt32_to_cpu(*val);
 
-	sbi_system_reset_add_device(&gpio_reset);
+	if (is_restart)
+		sbi_system_reset_add_device(&gpio_restart);
+	else
+		sbi_system_reset_add_device(&gpio_poweroff);
 
 	return 0;
 }
--

2.25.1

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

Applied
#2 lore raw

PATCH 1/1 lib: utils/reset: Register separate GPIO system reset devices

Quoted 10 lines · Dong Du
Quoted 5 lines · anup patel anup.patel at wdc.com

Now that sbi_system support multiple system reset devices, we should register separate devices for GPIO restart and GPIO poweroff because DT describes these as separate devices with different compatible strings.

Quoted 5 lines
Quoted 1 line

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 114 lines
Quoted 114 lines

---
lib/utils/reset/fdt_reset_gpio.c | 60 ++++++++++++++++++++++----------

1 file changed, 41 insertions(+), 19 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +41 -20
index 4da1450..302035b 100644
@@ -35,20 +35,20 @@ static struct gpio_reset restart = {
      .inactive_delay = 100
 };
 
-static struct gpio_reset *gpio_get_reset_settings(u32 type)
+static struct gpio_reset *gpio_reset_get(bool is_restart, u32 type)
 {
-     struct gpio_reset *reset;
+     struct gpio_reset *reset = NULL;
 
      switch (type) {
      case SBI_SRST_RESET_TYPE_SHUTDOWN:
-             reset = &poweroff;
+             if (!is_restart)
+                     reset = &poweroff;
              break;
      case SBI_SRST_RESET_TYPE_COLD_REBOOT:
      case SBI_SRST_RESET_TYPE_WARM_REBOOT:
-             reset = &restart;
+             if (is_restart)
+                     reset = &restart;
              break;
-     default:
-             reset = NULL;
      }
 
      if (reset && !reset->pin.chip)
@@ -57,15 +57,8 @@ static struct gpio_reset *gpio_get_reset_settings(u32 type)
      return reset;
 }
 
-static int gpio_system_reset_check(u32 type, u32 reason)
+static void gpio_reset_exec(struct gpio_reset *reset)
 {
-     return !!gpio_get_reset_settings(type);
-}
-
-static void gpio_system_reset(u32 type, u32 reason)
-{
-     struct gpio_reset *reset = gpio_get_reset_settings(type);
-
      if (reset) {
              /* drive it active, also inactive->active edge */
              gpio_direction_output(&reset->pin, 1);
@@ -82,10 +75,36 @@ static void gpio_system_reset(u32 type, u32 reason)
      sbi_hart_hang();
 }
 
-static struct sbi_system_reset_device gpio_reset = {
-     .name = "gpio-reset",
-     .system_reset_check = gpio_system_reset_check,
-     .system_reset = gpio_system_reset
+static int gpio_system_poweroff_check(u32 type, u32 reason)
+{
+     return !!gpio_reset_get(FALSE, type);
+}
+
+static void gpio_system_poweroff(u32 type, u32 reason)
+{
+     gpio_reset_exec(gpio_reset_get(FALSE, type));
+}
+
+static struct sbi_system_reset_device gpio_poweroff = {
+     .name = "gpio-poweroff",
+     .system_reset_check = gpio_system_poweroff_check,
+     .system_reset = gpio_system_poweroff
+};
+
+static int gpio_system_restart_check(u32 type, u32 reason)
+{
+     return !!gpio_reset_get(TRUE, type);
+}
+
+static void gpio_system_restart(u32 type, u32 reason)
+{
+     gpio_reset_exec(gpio_reset_get(TRUE, type));
+}
+
+static struct sbi_system_reset_device gpio_restart = {
+     .name = "gpio-restart",
+     .system_reset_check = gpio_system_restart_check,
+     .system_reset = gpio_system_restart
 };
 
 static int gpio_reset_init(void *fdt, int nodeoff,
@@ -115,7 +134,10 @@ static int gpio_reset_init(void *fdt, int nodeoff,
      if (len > 0)
              reset->inactive_delay = fdt32_to_cpu(*val);
 
-     sbi_system_reset_add_device(&gpio_reset);
+     if (is_restart)
+             sbi_system_reset_add_device(&gpio_restart);
+     else
+             sbi_system_reset_add_device(&gpio_poweroff);
 
      return 0;
 }
--

2.25.1

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

#3 lore raw

PATCH 1/1 lib: utils/reset: Register separate GPIO system reset devices

In reply to Anup Patel

? 2021-10-21???? 11:20 +0530?Anup Patel???

Quoted 17 lines

Now that sbi_system support multiple system reset devices, we should register separate devices for GPIO restart and GPIO poweroff because DT describes these as separate devices with different compatible strings.

---
?lib/utils/reset/fdt_reset_gpio.c | 60 ++++++++++++++++++++++-------- --
?1 file changed, 41 insertions(+), 19 deletions(-)

lib/utils/reset/fdt_reset_gpio.c +0 -0
index 4da1450..302035b 100644
@@ -35,20 +35,20 @@ static struct gpio_reset restart = {
Quoted 3 lines

????????.inactive_delay = 100
?};
?

I think the key to separate reset and shutdown drivers is system_reset_check.The following code can be simplified.

Regards,
Xiang W

Quoted 1 line

-static struct gpio_reset *gpio_get_reset_settings(u32 type)

This function does not need to be modified

Quoted 45 lines

+static struct gpio_reset *gpio_reset_get(bool is_restart, u32 type) ?{
-???????struct gpio_reset *reset;
+???????struct gpio_reset *reset = NULL;
?
????????switch (type) {
????????case SBI_SRST_RESET_TYPE_SHUTDOWN:
-???????????????reset = &poweroff;
+???????????????if (!is_restart) +???????????????????????reset = &poweroff; ????????????????break;
????????case SBI_SRST_RESET_TYPE_COLD_REBOOT:
????????case SBI_SRST_RESET_TYPE_WARM_REBOOT:
-???????????????reset = &restart;
+???????????????if (is_restart) +???????????????????????reset = &restart; ????????????????break;
-???????default:
-???????????????reset = NULL;
????????}
?
????????if (reset && !reset->pin.chip)

lib/utils/reset/fdt_reset_gpio.c +0 -0
@@ -57,15 +57,8 @@ static struct gpio_reset
 *gpio_get_reset_settings(u32 type)
 ????????return reset;
 ?}

?

lib/utils/reset/fdt_reset_gpio.c +1 -1
-static int gpio_system_reset_check(u32 type, u32 reason)
+static void gpio_reset_exec(struct gpio_reset *reset)

?{
-???????return !!gpio_get_reset_settings(type);

lib/utils/reset/fdt_reset_gpio.c +0 -6
-}
-
-static void gpio_system_reset(u32 type, u32 reason)
-{
-???????struct gpio_reset *reset = gpio_get_reset_settings(type);
-

????????if (reset) {
????????????????/* drive it active, also inactive->active edge */ ????????????????gpio_direction_output(&reset->pin, 1);

lib/utils/reset/fdt_reset_gpio.c +0 -0
@@ -82,10 +75,36 @@ static void gpio_system_reset(u32 type, u32
 reason)
 ????????sbi_hart_hang();
 ?}

?

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason) · line 79

Quoted 6 lines
lib/utils/reset/fdt_reset_gpio.c +2 -4
-static struct sbi_system_reset_device gpio_reset = {
-???????.name = "gpio-reset",
-???????.system_reset_check = gpio_system_reset_check,
-???????.system_reset = gpio_system_reset
+static int gpio_system_poweroff_check(u32 type, u32 reason)
+{

Add type check

	if (type == SBI_SRST_RESET_TYPE_SHUTDOWN)
		return !!gpio_get_reset_settings(type);
	return 0;

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason)

Quoted 11 lines

+???????return !!gpio_reset_get(FALSE, type);

lib/utils/reset/fdt_reset_gpio.c +10 -0
+}
+
+static void gpio_system_poweroff(u32 type, u32 reason)
+{
+???????gpio_reset_exec(gpio_reset_get(FALSE, type));
+}
+
+static struct sbi_system_reset_device gpio_poweroff = {
+???????.name = "gpio-poweroff",
+???????.system_reset_check = gpio_system_poweroff_check,

Keep general reset operation

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason) · line 100

Quoted 10 lines

+???????.system_reset = gpio_system_poweroff

lib/utils/reset/fdt_reset_gpio.c +9 -0
+};
+
+static int gpio_system_restart_check(u32 type, u32 reason)
+{
+???????return !!gpio_reset_get(TRUE, type);
+}
+
+static void gpio_system_restart(u32 type, u32 reason)
+{

Add type check

	if (type == SBI_SRST_RESET_TYPE_COLD_REBOOT?
		|| type == SBI_SRST_RESET_TYPE_WARM_REBOOT)
		return !!gpio_get_reset_settings(type);
	return 0;

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason)

Quoted 6 lines

+???????gpio_reset_exec(gpio_reset_get(TRUE, type));

lib/utils/reset/fdt_reset_gpio.c +5 -0
+}
+
+static struct sbi_system_reset_device gpio_restart = {
+???????.name = "gpio-restart",
+???????.system_reset_check = gpio_system_restart_check,

Keep general reset operation

Quoted 21 lines

+???????.system_reset = gpio_system_restart ?};
?
?static int gpio_reset_init(void *fdt, int nodeoff,

lib/utils/reset/fdt_reset_gpio.c +0 -0
@@ -115,7 +134,10 @@ static int gpio_reset_init(void *fdt, int
 nodeoff,
 ????????if (len > 0)
 ????????????????reset->inactive_delay = fdt32_to_cpu(*val);

? -???????sbi_system_reset_add_device(&gpio_reset); +???????if (is_restart) +???????????????sbi_system_reset_add_device(&gpio_restart); +???????else +???????????????sbi_system_reset_add_device(&gpio_poweroff); ?
????????return 0;
?}

Signature · 1 line
2.25.1
#4 lore raw

PATCH 1/1 lib: utils/reset: Register separate GPIO system reset devices

Quoted 13 lines · Xiang W

? 2021-10-21???? 11:20 +0530?Anup Patel???

Quoted 11 lines

Now that sbi_system support multiple system reset devices, we should register separate devices for GPIO restart and GPIO poweroff because DT describes these as separate devices with different compatible strings.

---
 lib/utils/reset/fdt_reset_gpio.c | 60 ++++++++++++++++++++++--------
--
 1 file changed, 41 insertions(+), 19 deletions(-)
Quoted 12 lines

lib/utils/reset/fdt_reset_gpio.c static struct gpio_reset restart = { · line 37

Quoted 9 lines
lib/utils/reset/fdt_reset_gpio.c +0 -0
index 4da1450..302035b 100644
@@ -35,20 +35,20 @@ static struct gpio_reset restart = {
        .inactive_delay = 100
 };
 

I think the key to separate reset and shutdown drivers is system_reset_check.The following code can be simplified.

Well, it's better/cleaner to have separate callbacks for GPIO restart and GPIO power-off such that restart callback never work for power-off and vice-versa.

Quoted 51 lines

Regards,
Xiang W

lib/utils/reset/fdt_reset_gpio.c static struct gpio_reset restart = { · line 38

Quoted 1 line
lib/utils/reset/fdt_reset_gpio.c +0 -1
-static struct gpio_reset *gpio_get_reset_settings(u32 type)

This function does not need to be modified

Quoted 45 lines
lib/utils/reset/fdt_reset_gpio.c +7 -13
+static struct gpio_reset *gpio_reset_get(bool is_restart, u32 type)
 {
-       struct gpio_reset *reset;
+       struct gpio_reset *reset = NULL;
 
        switch (type) {
        case SBI_SRST_RESET_TYPE_SHUTDOWN:
-               reset = &poweroff;
+               if (!is_restart)
+                       reset = &poweroff;
                break;
        case SBI_SRST_RESET_TYPE_COLD_REBOOT:
        case SBI_SRST_RESET_TYPE_WARM_REBOOT:
-               reset = &restart;
+               if (is_restart)
+                       reset = &restart;
                break;
-       default:
-               reset = NULL;
        }
 
        if (reset && !reset->pin.chip)
@@ -57,15 +57,8 @@ static struct gpio_reset
 *gpio_get_reset_settings(u32 type)
        return reset;
 }
 
-static int gpio_system_reset_check(u32 type, u32 reason)
+static void gpio_reset_exec(struct gpio_reset *reset)
 {
-       return !!gpio_get_reset_settings(type);
-}
-
-static void gpio_system_reset(u32 type, u32 reason)
-{
-       struct gpio_reset *reset = gpio_get_reset_settings(type);
-
        if (reset) {
                /* drive it active, also inactive->active edge */
                gpio_direction_output(&reset->pin, 1);
lib/utils/reset/fdt_reset_gpio.c +0 -0
@@ -82,10 +75,36 @@ static void gpio_system_reset(u32 type, u32
 reason)
        sbi_hart_hang();
 }
 
Quoted 7 lines

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason) · line 79

Quoted 6 lines
lib/utils/reset/fdt_reset_gpio.c +2 -4
-static struct sbi_system_reset_device gpio_reset = {
-       .name = "gpio-reset",
-       .system_reset_check = gpio_system_reset_check,
-       .system_reset = gpio_system_reset
+static int gpio_system_poweroff_check(u32 type, u32 reason)
+{

Add type check

IMO, we should keep the type checks in one place.

This will add redundant checks because gpio_get_reset_settings() already does the type checks so better to make gpio_get_reset_settings() more flexible which is what this patch does.

Quoted 15 lines
        if (type == SBI_SRST_RESET_TYPE_SHUTDOWN)
                return !!gpio_get_reset_settings(type);
        return 0;

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason) · line 90

Quoted 11 lines
lib/utils/reset/fdt_reset_gpio.c +11 -0
+       return !!gpio_reset_get(FALSE, type);
+}
+
+static void gpio_system_poweroff(u32 type, u32 reason)
+{
+       gpio_reset_exec(gpio_reset_get(FALSE, type));
+}
+
+static struct sbi_system_reset_device gpio_poweroff = {
+       .name = "gpio-poweroff",
+       .system_reset_check = gpio_system_poweroff_check,

Keep general reset operation

Quoted 11 lines

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason) · line 100

Quoted 10 lines
lib/utils/reset/fdt_reset_gpio.c +10 -0
+       .system_reset = gpio_system_poweroff
+};
+
+static int gpio_system_restart_check(u32 type, u32 reason)
+{
+       return !!gpio_reset_get(TRUE, type);
+}
+
+static void gpio_system_restart(u32 type, u32 reason)
+{

Add type check

Please see above comment.

Quoted 26 lines
        if (type == SBI_SRST_RESET_TYPE_COLD_REBOOT
                || type == SBI_SRST_RESET_TYPE_WARM_REBOOT)
                return !!gpio_get_reset_settings(type);
        return 0;

lib/utils/reset/fdt_reset_gpio.c static void gpio_system_reset(u32 type, u32 reason) · line 106

Quoted 6 lines
lib/utils/reset/fdt_reset_gpio.c +6 -0
+       gpio_reset_exec(gpio_reset_get(TRUE, type));
+}
+
+static struct sbi_system_reset_device gpio_restart = {
+       .name = "gpio-restart",
+       .system_reset_check = gpio_system_restart_check,

Keep general reset operation

Quoted 15 lines
lib/utils/reset/fdt_reset_gpio.c +1 -0
+       .system_reset = gpio_system_restart
 };
 

static int gpio_reset_init(void *fdt, int nodeoff,

lib/utils/reset/fdt_reset_gpio.c +4 -1
@@ -115,7 +134,10 @@ static int gpio_reset_init(void *fdt, int
 nodeoff,
        if (len > 0)
                reset->inactive_delay = fdt32_to_cpu(*val);
 
-       sbi_system_reset_add_device(&gpio_reset);
+       if (is_restart)
+               sbi_system_reset_add_device(&gpio_restart);
+       else
+               sbi_system_reset_add_device(&gpio_poweroff);
 
Quoted 8 lines
Quoted 6 lines
lib/utils/reset/fdt_reset_gpio.c +0 -0
        return 0;

}
--
2.25.1

Regards,
Anup