Skip to contentSkip to conversation

watchdog: pm8916_wdt: Some minor improvements

PATCH Superseded Stephan Gerhold 2 participants 7 messages Jun 27, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover watchdog: pm8916_wdt: Some minor improvements

Optimize the pm8916_wdt.c driver a bit by pinging the watchdog using a write instead of read-modify-write. Also report the reboot reason to userspace and (temporarily) ping the watchdog from the kernel if it was already enabled by the bootloader.

Stephan Gerhold (3):
watchdog: pm8916_wdt: Avoid read of write-only PET register
watchdog: pm8916_wdt: Report reboot reason
watchdog: pm8916_wdt: Handle watchdog enabled by bootloader

 drivers/watchdog/pm8916_wdt.c | 36 +++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 watchdog: pm8916_wdt: Avoid read of write-only PET register

PMIC_WD_RESET_PET is a write-only register that is used to ping the watchdog. It does not make sense to use read-modify-write for it: a register read will never return anything but zero. (And actually even if it did we would still want to write again to ensure the watchdog is pinged.)

Reduce the overhead for the watchdog ping a bit by using regmap_write() directly instead.

---
 drivers/watchdog/pm8916_wdt.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/watchdog/pm8916_wdt.c +2 -3
index 0937b8d33104..670cd79f4cf9 100644
@@ -58,9 +58,8 @@ static int pm8916_wdt_ping(struct watchdog_device *wdev)
 {
 	struct pm8916_wdt *wdt = watchdog_get_drvdata(wdev);
 
-	return regmap_update_bits(wdt->regmap,
-				  wdt->baseaddr + PON_PMIC_WD_RESET_PET,
-				  WATCHDOG_PET_BIT, WATCHDOG_PET_BIT);
+	return regmap_write(wdt->regmap, wdt->baseaddr + PON_PMIC_WD_RESET_PET,
+			    WATCHDOG_PET_BIT);
 }
 
 static int pm8916_wdt_configure_timers(struct watchdog_device *wdev)
Signature · 1 line
2.30.2
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] watchdog: pm8916_wdt: Avoid read of write-only PET register

Quoted 10 lines · Stephan Gerhold

PMIC_WD_RESET_PET is a write-only register that is used to ping the watchdog. It does not make sense to use read-modify-write for it: a register read will never return anything but zero. (And actually even if it did we would still want to write again to ensure the watchdog is pinged.)

Reduce the overhead for the watchdog ping a bit by using regmap_write() directly instead.

Quoted 20 lines
---
  drivers/watchdog/pm8916_wdt.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/watchdog/pm8916_wdt.c +2 -3
index 0937b8d33104..670cd79f4cf9 100644
@@ -58,9 +58,8 @@ static int pm8916_wdt_ping(struct watchdog_device *wdev)
  {
  	struct pm8916_wdt *wdt = watchdog_get_drvdata(wdev);
 
-	return regmap_update_bits(wdt->regmap,
-				  wdt->baseaddr + PON_PMIC_WD_RESET_PET,
-				  WATCHDOG_PET_BIT, WATCHDOG_PET_BIT);
+	return regmap_write(wdt->regmap, wdt->baseaddr + PON_PMIC_WD_RESET_PET,
+			    WATCHDOG_PET_BIT);
  }
 
  static int pm8916_wdt_configure_timers(struct watchdog_device *wdev)
#3 lore raw

PATCH 2/3 watchdog: pm8916_wdt: Report reboot reason

In reply to Stephan Gerhold

The PM8916 PMIC provides "power-off reason" (POFF_REASON) registers to allow detecting why the board was powered off or rebooted. This can be used to expose if a reset happened due to a watchdog timeout. The watchdog API also provides status bits for overtemperature and undervoltage which happen to be reported in the same PMIC register.

Make this information available as part of the watchdog device so userspace can decide to handle the situation accordingly.

---
 drivers/watchdog/pm8916_wdt.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/watchdog/pm8916_wdt.c +24 -1
index 670cd79f4cf9..3fc03fdae466 100644
@@ -9,6 +9,12 @@
 #include <linux/regmap.h>
 #include <linux/watchdog.h>
 
+#define PON_POFF_REASON1		0x0c
+#define PON_POFF_REASON1_PMIC_WD	BIT(2)
+#define PON_POFF_REASON2		0x0d
+#define PON_POFF_REASON2_UVLO		BIT(5)
+#define PON_POFF_REASON2_OTST3		BIT(6)
+
 #define PON_INT_RT_STS			0x10
 #define PMIC_WD_BARK_STS_BIT		BIT(6)
 
@@ -110,12 +116,14 @@ static irqreturn_t pm8916_wdt_isr(int irq, void *arg)
 }
 
 static const struct watchdog_info pm8916_wdt_ident = {
-	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
+	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE |
+		   WDIOF_OVERHEAT | WDIOF_CARDRESET | WDIOF_POWERUNDER,
 	.identity = "QCOM PM8916 PON WDT",
 };
 
 static const struct watchdog_info pm8916_wdt_pt_ident = {
 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE |
+		   WDIOF_OVERHEAT | WDIOF_CARDRESET | WDIOF_POWERUNDER |
 		   WDIOF_PRETIMEOUT,
 	.identity = "QCOM PM8916 PON WDT",
 };
@@ -135,6 +143,7 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
 	struct pm8916_wdt *wdt;
 	struct device *parent;
 	int err, irq;
+	u8 poff[2];
 
 	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
 	if (!wdt)
@@ -175,6 +184,20 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
 		wdt->wdev.info = &pm8916_wdt_ident;
 	}
 
+	err = regmap_bulk_read(wdt->regmap, wdt->baseaddr + PON_POFF_REASON1,
+			       &poff, ARRAY_SIZE(poff));
+	if (err == 0) {
+		dev_dbg(dev, "POFF reason: %#x %#x\n", poff[0], poff[1]);
+		if (poff[0] & PON_POFF_REASON1_PMIC_WD)
+			wdt->wdev.bootstatus |= WDIOF_CARDRESET;
+		if (poff[1] & PON_POFF_REASON2_UVLO)
+			wdt->wdev.bootstatus |= WDIOF_POWERUNDER;
+		if (poff[1] & PON_POFF_REASON2_OTST3)
+			wdt->wdev.bootstatus |= WDIOF_OVERHEAT;
+	} else {
+		dev_err(dev, "failed to read POFF reason: %d\n", err);
+	}
+
 	/* Configure watchdog to hard-reset mode */
 	err = regmap_write(wdt->regmap,
 			   wdt->baseaddr + PON_PMIC_WD_RESET_S2_CTL,
Signature · 1 line
2.30.2
maintainer
#4 lore raw

Re: [PATCH 2/3] watchdog: pm8916_wdt: Report reboot reason

Quoted 62 lines · Stephan Gerhold

The PM8916 PMIC provides "power-off reason" (POFF_REASON) registers to allow detecting why the board was powered off or rebooted. This can be used to expose if a reset happened due to a watchdog timeout. The watchdog API also provides status bits for overtemperature and undervoltage which happen to be reported in the same PMIC register.

Make this information available as part of the watchdog device so userspace can decide to handle the situation accordingly.

---
  drivers/watchdog/pm8916_wdt.c | 25 ++++++++++++++++++++++++-
  1 file changed, 24 insertions(+), 1 deletion(-)
drivers/watchdog/pm8916_wdt.c +13 -1
index 670cd79f4cf9..3fc03fdae466 100644
@@ -9,6 +9,12 @@
  #include <linux/regmap.h>
  #include <linux/watchdog.h>
 
+#define PON_POFF_REASON1		0x0c
+#define PON_POFF_REASON1_PMIC_WD	BIT(2)
+#define PON_POFF_REASON2		0x0d
+#define PON_POFF_REASON2_UVLO		BIT(5)
+#define PON_POFF_REASON2_OTST3		BIT(6)
+
  #define PON_INT_RT_STS			0x10
  #define PMIC_WD_BARK_STS_BIT		BIT(6)
 
@@ -110,12 +116,14 @@ static irqreturn_t pm8916_wdt_isr(int irq, void *arg)
  }
 
  static const struct watchdog_info pm8916_wdt_ident = {
-	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
+	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE |
+		   WDIOF_OVERHEAT | WDIOF_CARDRESET | WDIOF_POWERUNDER,
  	.identity = "QCOM PM8916 PON WDT",
  };
 
  static const struct watchdog_info pm8916_wdt_pt_ident = {
  	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE |
+		   WDIOF_OVERHEAT | WDIOF_CARDRESET | WDIOF_POWERUNDER |
  		   WDIOF_PRETIMEOUT,
  	.identity = "QCOM PM8916 PON WDT",
  };
@@ -135,6 +143,7 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
  	struct pm8916_wdt *wdt;
  	struct device *parent;
  	int err, irq;
+	u8 poff[2];
 
  	wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
  	if (!wdt)
@@ -175,6 +184,20 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
  		wdt->wdev.info = &pm8916_wdt_ident;
  	}
 
+	err = regmap_bulk_read(wdt->regmap, wdt->baseaddr + PON_POFF_REASON1,
+			       &poff, ARRAY_SIZE(poff));
+	if (err == 0) {

drivers/watchdog/pm8916_wdt.c static int pm8916_wdt_probe(struct platform_device *pdev) · line 199

Quoted 10 lines
drivers/watchdog/pm8916_wdt.c +10 -0
+		dev_dbg(dev, "POFF reason: %#x %#x\n", poff[0], poff[1]);
+		if (poff[0] & PON_POFF_REASON1_PMIC_WD)
+			wdt->wdev.bootstatus |= WDIOF_CARDRESET;
+		if (poff[1] & PON_POFF_REASON2_UVLO)
+			wdt->wdev.bootstatus |= WDIOF_POWERUNDER;
+		if (poff[1] & PON_POFF_REASON2_OTST3)
+			wdt->wdev.bootstatus |= WDIOF_OVERHEAT;
+	} else {
+		dev_err(dev, "failed to read POFF reason: %d\n", err);
+	}

Error handling first, please:

	if (err) {
	} else {
	}

Also, decide if this is indeed an error or not. If it is an error, abort probe. Otherwise use dev_warn() and explain why the problem is ignored / not fatal.

Thanks,
Guenter

Quoted 4 lines
drivers/watchdog/pm8916_wdt.c +1 -0
+
  	/* Configure watchdog to hard-reset mode */
  	err = regmap_write(wdt->regmap,
  			   wdt->baseaddr + PON_PMIC_WD_RESET_S2_CTL,
#5 lore raw

PATCH 3/3 watchdog: pm8916_wdt: Handle watchdog enabled by bootloader

In reply to Stephan Gerhold

The bootloader might already enable the watchdog to catch hangs during the boot process. In that case the kernel needs to ping the watchdog temporarily until userspace is fully started.

Add a check for this in the probe() function and set the WDOG_HW_RUNNING flag to make the watchdog core handle this automatically.

---
 drivers/watchdog/pm8916_wdt.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/watchdog/pm8916_wdt.c +6 -0
index 3fc03fdae466..4ebd1d0cab85 100644
@@ -142,6 +142,7 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct pm8916_wdt *wdt;
 	struct device *parent;
+	unsigned int val;
 	int err, irq;
 	u8 poff[2];
 
@@ -198,6 +199,11 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
 		dev_err(dev, "failed to read POFF reason: %d\n", err);
 	}
 
+	err = regmap_read(wdt->regmap, wdt->baseaddr + PON_PMIC_WD_RESET_S2_CTL2,
+			  &val);
+	if (err == 0 && val & S2_RESET_EN_BIT)
+		set_bit(WDOG_HW_RUNNING, &wdt->wdev.status);
+
 	/* Configure watchdog to hard-reset mode */
 	err = regmap_write(wdt->regmap,
 			   wdt->baseaddr + PON_PMIC_WD_RESET_S2_CTL,
Signature · 1 line
2.30.2
maintainer
#6 lore raw

Re: [PATCH 3/3] watchdog: pm8916_wdt: Handle watchdog enabled by bootloader

Quoted 25 lines · Stephan Gerhold

The bootloader might already enable the watchdog to catch hangs during the boot process. In that case the kernel needs to ping the watchdog temporarily until userspace is fully started.

Add a check for this in the probe() function and set the WDOG_HW_RUNNING flag to make the watchdog core handle this automatically.

---
  drivers/watchdog/pm8916_wdt.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/watchdog/pm8916_wdt.c +1 -0
index 3fc03fdae466..4ebd1d0cab85 100644
@@ -142,6 +142,7 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
  	struct device *dev = &pdev->dev;
  	struct pm8916_wdt *wdt;
  	struct device *parent;
+	unsigned int val;
  	int err, irq;
  	u8 poff[2];
 

drivers/watchdog/pm8916_wdt.c static int pm8916_wdt_probe(struct platform_device *pdev) · line 206

Quoted 8 lines
drivers/watchdog/pm8916_wdt.c +5 -0
@@ -198,6 +199,11 @@ static int pm8916_wdt_probe(struct platform_device *pdev)
  		dev_err(dev, "failed to read POFF reason: %d\n", err);
  	}
 
+	err = regmap_read(wdt->regmap, wdt->baseaddr + PON_PMIC_WD_RESET_S2_CTL2,
+			  &val);
+	if (err == 0 && val & S2_RESET_EN_BIT)
+		set_bit(WDOG_HW_RUNNING, &wdt->wdev.status);
+

If reading the status returns an error, I'd assume that something is seriously wrong. If it is ok to ignore such errors, explain why this is the case.

Thanks,
Guenter

Quoted 3 lines
drivers/watchdog/pm8916_wdt.c +0 -0
  	/* Configure watchdog to hard-reset mode */
  	err = regmap_write(wdt->regmap,
  			   wdt->baseaddr + PON_PMIC_WD_RESET_S2_CTL,