Skip to contentSkip to conversation

watchdog: imx7ulp: Strictly follow the sequence for wdog operations

PATCH Superseded Anson Huang 2 participants 13 messages Jul 29, 2020 – Jul 30, 2020

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 watchdog: imx7ulp: Strictly follow the sequence for wdog operations

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set;
3. reconfigure the wdog and wait for reconfiguration bit set;
4. enabel global interrupts.

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since IRQ is disabled.

---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
drivers/watchdog/imx7ulp_wdt.c +29 -0
index 7993c8c..7d2b12e 100644
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, bool, 0000);
@@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
 	struct clk *clk;
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask)
+{
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));
+}
+
 static void imx7ulp_wdt_enable(struct watchdog_device *wdog, bool enable)
 {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
 	u32 val = readl(wdt->base + WDOG_CS);
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	if (enable)
 		writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
 	else
 		writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 
 static bool imx7ulp_wdt_is_enabled(void __iomem *base)
@@ -72,7 +88,12 @@ static int imx7ulp_wdt_ping(struct watchdog_device *wdog)
 {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
+	local_irq_disable();
+	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(REFRESH, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 
 	return 0;
 }
@@ -98,8 +119,12 @@ static int imx7ulp_wdt_set_timeout(struct watchdog_device *wdog,
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 	u32 val = WDOG_CLOCK_RATE * timeout;
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(val, wdt->base + WDOG_TOVAL);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 
 	wdog->timeout = timeout;
 
@@ -140,15 +165,19 @@ static void imx7ulp_wdt_init(void __iomem *base, unsigned int timeout)
 {
 	u32 val;
 
+	local_irq_disable();
 	/* unlock the wdog for reconfiguration */
 	writel_relaxed(UNLOCK_SEQ0, base + WDOG_CNT);
 	writel_relaxed(UNLOCK_SEQ1, base + WDOG_CNT);
+	imx7ulp_wdt_wait(base, WDOG_CS_ULK);
 
 	/* set an initial timeout value in TOVAL */
 	writel(timeout, base + WDOG_TOVAL);
 	/* enable 32bit command sequence and reconfigure */
 	val = WDOG_CS_CMD32EN | WDOG_CS_CLK | WDOG_CS_UPDATE;
 	writel(val, base + WDOG_CS);
+	imx7ulp_wdt_wait(base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 
 static void imx7ulp_wdt_action(void *data)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 2/2 watchdog: imx7ulp: Watchdog should continue running for wait/stop mode

When kernel idle, system will enter wait/stop mode, wdog should continue running in this scenario, and the refresh thread can wake up system from wait/stop mode.

---
no change.

---
 drivers/watchdog/imx7ulp_wdt.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/watchdog/imx7ulp_wdt.c +4 -1
index 7d2b12e..2b4ff43 100644
@@ -22,6 +22,8 @@
 #define WDOG_CS_CLK		(LPO_CLK << LPO_CLK_SHIFT)
 #define WDOG_CS_EN		BIT(7)
 #define WDOG_CS_UPDATE		BIT(5)
+#define WDOG_CS_WAIT		BIT(1)
+#define WDOG_CS_STOP		BIT(0)
 
 #define WDOG_CNT	0x4
 #define WDOG_TOVAL	0x8
@@ -174,7 +176,8 @@ static void imx7ulp_wdt_init(void __iomem *base, unsigned int timeout)
 	/* set an initial timeout value in TOVAL */
 	writel(timeout, base + WDOG_TOVAL);
 	/* enable 32bit command sequence and reconfigure */
-	val = WDOG_CS_CMD32EN | WDOG_CS_CLK | WDOG_CS_UPDATE;
+	val = WDOG_CS_CMD32EN | WDOG_CS_CLK | WDOG_CS_UPDATE |
+	      WDOG_CS_WAIT | WDOG_CS_STOP;
 	writel(val, base + WDOG_CS);
 	imx7ulp_wdt_wait(base, WDOG_CS_RCS);
 	local_irq_enable();
Signature · 1 line
2.7.4
maintainer
#2 lore raw
In reply to Anson Huang
Quoted 40 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set;
3. reconfigure the wdog and wait for reconfiguration bit set;
4. enabel global interrupts.

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since IRQ is disabled.

---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
drivers/watchdog/imx7ulp_wdt.c +2 -0
index 7993c8c..7d2b12e 100644
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, bool, 0000);
@@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
 	struct clk *clk;

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device { · line 60

Quoted 10 lines
drivers/watchdog/imx7ulp_wdt.c +8 -0
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask)
+{
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));

I am not a friend of WARN_ON, especially in situations like this. Please explain why this is needed, and why a return of -ETIMEDOUT is not feasible.

Also, I do not believe that a 10 milli-second timeout is warranted. This will need to be backed up by the datasheet.

Guenter

Quoted 67 lines
drivers/watchdog/imx7ulp_wdt.c +19 -0
+}
+
 static void imx7ulp_wdt_enable(struct watchdog_device *wdog, bool enable)
 {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
 	u32 val = readl(wdt->base + WDOG_CS);
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	if (enable)
 		writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
 	else
 		writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 
 static bool imx7ulp_wdt_is_enabled(void __iomem *base)
@@ -72,7 +88,12 @@ static int imx7ulp_wdt_ping(struct watchdog_device *wdog)
 {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
+	local_irq_disable();
+	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(REFRESH, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 
 	return 0;
 }
@@ -98,8 +119,12 @@ static int imx7ulp_wdt_set_timeout(struct watchdog_device *wdog,
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 	u32 val = WDOG_CLOCK_RATE * timeout;
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(val, wdt->base + WDOG_TOVAL);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 
 	wdog->timeout = timeout;
 
@@ -140,15 +165,19 @@ static void imx7ulp_wdt_init(void __iomem *base, unsigned int timeout)
 {
 	u32 val;
 
+	local_irq_disable();
 	/* unlock the wdog for reconfiguration */
 	writel_relaxed(UNLOCK_SEQ0, base + WDOG_CNT);
 	writel_relaxed(UNLOCK_SEQ1, base + WDOG_CNT);
+	imx7ulp_wdt_wait(base, WDOG_CS_ULK);
 
 	/* set an initial timeout value in TOVAL */
 	writel(timeout, base + WDOG_TOVAL);
 	/* enable 32bit command sequence and reconfigure */
 	val = WDOG_CS_CMD32EN | WDOG_CS_CLK | WDOG_CS_UPDATE;
 	writel(val, base + WDOG_CS);
+	imx7ulp_wdt_wait(base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 
 static void imx7ulp_wdt_action(void *data)
#3 lore raw

Hi, Guenter

Quoted 46 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 6 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

Quoted 4 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {

struct clk *clk;
};

Quoted 11 lines

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device { · line 60

Quoted 7 lines
drivers/watchdog/imx7ulp_wdt.c +7 -0
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));

I am not a friend of WARN_ON, especially in situations like this. Please explain why this is needed, and why a return of -ETIMEDOUT is not feasible.

OK, I will use return value of -ETIMEOUT and handle it in the caller.

Quoted 4 lines

Also, I do not believe that a 10 milli-second timeout is warranted. This will need to be backed up by the datasheet.

There is no such info provided in reference manual or datasheet, but I just did an experiment, the unlock window is open in less than 1us after sending unlock command, and ONLY last for ONLY 2~3 us then close, the reconfiguration status bit will be set in less than 1us after register write. So what do you recommend for this timeout value? 100mS for safe?

Thanks,
Anson

#4 lore raw

Hi, Guenter

Quoted 65 lines

Subject: RE: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Hi, Guenter

Quoted 47 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 7 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since
IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

Quoted 4 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {

struct clk *clk;
};

Quoted 11 lines

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device { · line 60

Quoted 7 lines
drivers/watchdog/imx7ulp_wdt.c +7 -0
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));

I am not a friend of WARN_ON, especially in situations like this. Please explain why this is needed, and why a return of -ETIMEDOUT is not feasible.

Quoted 1 line

OK, I will use return value of -ETIMEOUT and handle it in the caller.

After a further look, some of the imx7ulp_wdt_wait () callers are void function, so if want to handle the return value, all those functions return type need to be changed. And, when the return value is -ETIMEDOUT, the ONLY action is to print out some error message for these void function, need to use pr_err() due to no dev pointer available, so do you think it is acceptable to just replace the WARN_ON with pr_err() as below?

+ if (!(val & mask)) {
+ if (readl_poll_timeout_atomic(base + WDOG_CS, val,
+ val & mask, 0,
+ WDOG_WAIT_TIMEOUT))
+ pr_err("wdog wait timeout, mask 0x%x\n", mask);
+ }

Thanks,
Anson

maintainer
#5 lore raw
Quoted 68 lines · Anson Huang

Hi, Guenter

Quoted 65 lines

Subject: RE: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Hi, Guenter

Quoted 58 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 7 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since
IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device { · line 60

Quoted 11 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
drivers/watchdog/imx7ulp_wdt.c +7 -0
 	struct clk *clk;
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));

I am not a friend of WARN_ON, especially in situations like this. Please explain why this is needed, and why a return of -ETIMEDOUT is not feasible.

Quoted 8 lines
Quoted 1 line

OK, I will use return value of -ETIMEOUT and handle it in the caller.

After a further look, some of the imx7ulp_wdt_wait () callers are void function, so if want to handle the return value, all those functions return type need to be changed. And, when the return value is -ETIMEDOUT, the ONLY action is to print out some error message for these void function, need to use pr_err() due to no dev pointer available, so do you think it is acceptable to just replace the WARN_ON with pr_err() as below?

First, the point here is that the callers can't do their work if the function times out. So, if the return value isn't necessary, and callers don't need to check it, the function would not be necessary to start with. If it is necessary, and if there is a concern that it can fail, callers should make sure that it actually succeeded. With that in mind, yes, imx7ulp_wdt_init() should fail and return an error, because presumably that is what happened. The same is true for imx7ulp_wdt_enable(). Really, what is the point of detecting a problem just to ignore it ?

Second, the wait function is also called _after_ a register was set. In many cases that won't do any good or bad. While it is ok to ignore the error in that case (when nothing else is done), the error message is pointless in that situation.

Thanks,
Guenter

#6 lore raw

Hi, Guenter

Quoted 83 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 30 lines · Anson Huang

Hi, Guenter

Quoted 27 lines

Subject: RE: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Hi, Guenter

Quoted 21 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 7 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since
IRQ is

disabled.

Quoted 2 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29

+++++++++++++++++++++++++++++

Quoted 38 lines
Quoted 38 lines
Quoted 37 lines
Quoted 15 lines
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device { · line 60

Quoted 11 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
drivers/watchdog/imx7ulp_wdt.c +7 -0
 	struct clk *clk;
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));

I am not a friend of WARN_ON, especially in situations like this. Please explain why this is needed, and why a return of -ETIMEDOUT is not feasible.

Quoted 7 lines
Quoted 1 line

OK, I will use return value of -ETIMEOUT and handle it in the caller.

After a further look, some of the imx7ulp_wdt_wait () callers are void function, so if want to handle the return value, all those functions return type need to be changed. And, when the return value is -ETIMEDOUT, the ONLY action is to print out some error message for these void function, need to use pr_err() due to no dev pointer available, so

do you think it is acceptable to just replace the WARN_ON with pr_err() as below?

Quoted 1 line
Quoted 8 lines

First, the point here is that the callers can't do their work if the function times out. So, if the return value isn't necessary, and callers don't need to check it, the function would not be necessary to start with. If it is necessary, and if there is a concern that it can fail, callers should make sure that it actually succeeded. With that in mind, yes, imx7ulp_wdt_init() should fail and return an error, because presumably that is what happened. The same is true for imx7ulp_wdt_enable().
Really, what is the point of detecting a problem just to ignore it ?

Understood, then I will change those void caller functions to int type, and return error if timeout. And maybe some 'goto' will be added, since whenever error happen, it has to go to enable local irq then return.

Quoted 5 lines

Second, the wait function is also called _after_ a register was set. In many cases that won't do any good or bad. While it is ok to ignore the error in that case (when nothing else is done), the error message is pointless in that situation.

OK, for the RCS bit check, I will just ignore the return value check, similar with the clk_disable_unprepare(), nothing can be done for such scenario.

Thanks,
Anson

maintainer
#7 lore raw
In reply to Anson Huang
Quoted 64 lines · Anson Huang

Hi, Guenter

Quoted 46 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 6 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

Quoted 4 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {

struct clk *clk;
};

Quoted 11 lines

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device { · line 60

Quoted 7 lines
drivers/watchdog/imx7ulp_wdt.c +7 -0
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));

I am not a friend of WARN_ON, especially in situations like this. Please explain why this is needed, and why a return of -ETIMEDOUT is not feasible.

OK, I will use return value of -ETIMEOUT and handle it in the caller.

Quoted 1 line
Quoted 9 lines
Quoted 3 lines

Also, I do not believe that a 10 milli-second timeout is warranted. This will need to be backed up by the datasheet.

There is no such info provided in reference manual or datasheet, but I just did an experiment, the unlock window is open in less than 1us after sending unlock command, and ONLY last for ONLY 2~3 us then close, the reconfiguration status bit will be set in less than 1us after register write. So what do you recommend for this timeout value? 100mS for safe?

That would be even worse. You say yourself that the window is only open for a few microseconds. Now you are suggesting to hold the entire system hostage for up to 100 mS if the code misses that window for some reason. Based on what you said, 100 uS might be barely acceptable. 10-20 uS would be reasonable. But not 100 mS.

Guenter

#8 lore raw

Hi, Guenter

Quoted 80 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 65 lines · Anson Huang

Hi, Guenter

Quoted 47 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 7 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since
IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

Quoted 4 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {

struct clk *clk;
};

Quoted 11 lines

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device { · line 60

Quoted 7 lines
drivers/watchdog/imx7ulp_wdt.c +7 -0
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));

I am not a friend of WARN_ON, especially in situations like this. Please explain why this is needed, and why a return of -ETIMEDOUT is not feasible.

OK, I will use return value of -ETIMEOUT and handle it in the caller.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

Also, I do not believe that a 10 milli-second timeout is warranted. This will need to be backed up by the datasheet.

There is no such info provided in reference manual or datasheet, but I just did an experiment, the unlock window is open in less than 1us after sending unlock command, and ONLY last for ONLY 2~3 us then close, the reconfiguration status bit will be set in less than 1us after register

write. So what do you recommend for this timeout value? 100mS for safe?

Quoted 1 line
Quoted 7 lines

That would be even worse. You say yourself that the window is only open for a few microseconds. Now you are suggesting to hold the entire system hostage for up to
100 mS if the code misses that window for some reason. Based on what you said,
100 uS might be barely acceptable. 10-20 uS would be reasonable. But not 100 mS.

OK, I will use 20us.

Thanks,
Anson

maintainer
#9 lore raw
In reply to Anson Huang
Quoted 71 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set;
3. reconfigure the wdog and wait for reconfiguration bit set;
4. enabel global interrupts.

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since IRQ is disabled.

---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
drivers/watchdog/imx7ulp_wdt.c +16 -0
index 7993c8c..7d2b12e 100644
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, bool, 0000);
@@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
 	struct clk *clk;
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask)
+{
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));
+}
+
 static void imx7ulp_wdt_enable(struct watchdog_device *wdog, bool enable)
 {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
 	u32 val = readl(wdt->base + WDOG_CS);
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	if (enable)
 		writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
 	else
 		writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 
 static bool imx7ulp_wdt_is_enabled(void __iomem *base)

drivers/watchdog/imx7ulp_wdt.c static int imx7ulp_wdt_ping(struct watchdog_device *wdog) · line 95

Quoted 8 lines
drivers/watchdog/imx7ulp_wdt.c +4 -0
@@ -72,7 +88,12 @@ static int imx7ulp_wdt_ping(struct watchdog_device *wdog)
 {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
+	local_irq_disable();
+	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(REFRESH, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);

Per reference manual (section 59.5.4), the waits are not required here, and neither is the unlock. For practical purposes, disabling interrupts is useless as well since the refresh write operation is just a single register write.

Guenter

#10 lore raw

Hi, Guenter

Quoted 76 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 6 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

Quoted 34 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
drivers/watchdog/imx7ulp_wdt.c +9 -0
 	struct clk *clk;
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));
+}
+
 static void imx7ulp_wdt_enable(struct watchdog_device *wdog, bool

enable) {

drivers/watchdog/imx7ulp_wdt.c +5 -0
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
 	u32 val = readl(wdt->base + WDOG_CS);
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	if (enable)
 		writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
 	else
 		writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 
 static bool imx7ulp_wdt_is_enabled(void __iomem *base) @@ -72,7
+88,12 @@ static int imx7ulp_wdt_ping(struct watchdog_device *wdog)  {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
Quoted 9 lines

drivers/watchdog/imx7ulp_wdt.c static int imx7ulp_wdt_ping(struct watchdog_device *wdog) · line 95

Quoted 5 lines
drivers/watchdog/imx7ulp_wdt.c +4 -0
+	local_irq_disable();
+	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(REFRESH, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);

Per reference manual (section 59.5.4), the waits are not required here, and neither is the unlock. For practical purposes, disabling interrupts is useless as well since the refresh write operation is just a single register write.

Correct, the example in reference manual does NOT have this flow for refresh, but I checked with our design team yestoday, their validation code indeed has this flow, that is why I added it for refresh operation as well.
I will do a test on our EVK board, and if it works without this flow, I will remove them In V3.

Thanks,
Anson

maintainer
#11 lore raw
Quoted 89 lines · Anson Huang

Hi, Guenter

Quoted 76 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 6 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

Quoted 34 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
drivers/watchdog/imx7ulp_wdt.c +9 -0
 	struct clk *clk;
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));
+}
+
 static void imx7ulp_wdt_enable(struct watchdog_device *wdog, bool

enable) {

drivers/watchdog/imx7ulp_wdt.c +5 -0
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
 	u32 val = readl(wdt->base + WDOG_CS);
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	if (enable)
 		writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
 	else
 		writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 
 static bool imx7ulp_wdt_is_enabled(void __iomem *base) @@ -72,7
+88,12 @@ static int imx7ulp_wdt_ping(struct watchdog_device *wdog)  {
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
Quoted 9 lines

drivers/watchdog/imx7ulp_wdt.c static int imx7ulp_wdt_ping(struct watchdog_device *wdog) · line 95

Quoted 5 lines
drivers/watchdog/imx7ulp_wdt.c +4 -0
+	local_irq_disable();
+	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(REFRESH, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);

Per reference manual (section 59.5.4), the waits are not required here, and neither is the unlock. For practical purposes, disabling interrupts is useless as well since the refresh write operation is just a single register write.

Quoted 3 lines

Correct, the example in reference manual does NOT have this flow for refresh, but I checked with our design team yestoday, their validation code indeed has this flow, that is why I added it for refresh operation as well.

If it is needed, they'll need to update the manual. Urgently. Really, missing the information that the watchdog must be unlocked in order to refresh the timer would not just be be a minor flaw in the manual. Either it is needed and must be mentioned (because the watchdog would otherwise simply not work), or it isn't needed and should not be done.

Thanks,
Guenter

Quoted 9 lines

I will do a test on our EVK board, and if it works without this flow, I will remove them In V3.

Thanks,
Anson

#12 lore raw

Hi, Guenter

Quoted 100 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 90 lines · Anson Huang

Hi, Guenter

Quoted 87 lines

Subject: Re: [PATCH V2 1/2] watchdog: imx7ulp: Strictly follow the sequence for wdog operations

Quoted 7 lines · Anson Huang

According to reference manual, the i.MX7ULP WDOG's operations should follow below sequence:

1. disable global interrupts;
2. unlock the wdog and wait unlock bit set; 3. reconfigure the wdog and wait for reconfiguration bit set; 4. enabel global interrupts.

Quoted 7 lines

Strictly follow the recommended sequence can make it more robust.

---
Changes since V1:
- use readl_poll_timeout_atomic() instead of usleep_ranges() since
IRQ is

disabled.

Quoted 17 lines
---
 drivers/watchdog/imx7ulp_wdt.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
(quoted diff) +0 -0

b/drivers/watchdog/imx7ulp_wdt.c index 7993c8c..7d2b12e 100644

drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -5,6 +5,7 @@
 
 #include <linux/clk.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>

drivers/watchdog/imx7ulp_wdt.c line 42

Quoted 6 lines
drivers/watchdog/imx7ulp_wdt.c +1 -0
@@ -36,6 +37,7 @@
 #define DEFAULT_TIMEOUT	60
 #define MAX_TIMEOUT	128
 #define WDOG_CLOCK_RATE	1000
+#define WDOG_WAIT_TIMEOUT	10000
 
 static bool nowayout = WATCHDOG_NOWAYOUT;

module_param(nowayout,

Quoted 30 lines
drivers/watchdog/imx7ulp_wdt.c +0 -0
 bool, 0000); @@ -48,17 +50,31 @@ struct imx7ulp_wdt_device {
drivers/watchdog/imx7ulp_wdt.c +9 -0
 	struct clk *clk;
 };
 
+static inline void imx7ulp_wdt_wait(void __iomem *base, u32 mask) {
+	u32 val = readl(base + WDOG_CS);
+
+	if (!(val & mask))
+		WARN_ON(readl_poll_timeout_atomic(base + WDOG_CS, val,
+						  val & mask, 0,
+						  WDOG_WAIT_TIMEOUT));
+}
+
 static void imx7ulp_wdt_enable(struct watchdog_device *wdog, bool

enable) {

drivers/watchdog/imx7ulp_wdt.c +4 -0
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
 	u32 val = readl(wdt->base + WDOG_CS);
 
+	local_irq_disable();
 	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	if (enable)
 		writel(val | WDOG_CS_EN, wdt->base + WDOG_CS);
 	else
 		writel(val & ~WDOG_CS_EN, wdt->base + WDOG_CS);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);
+	local_irq_enable();
 }
 

drivers/watchdog/imx7ulp_wdt.c struct imx7ulp_wdt_device {

Quoted 10 lines
drivers/watchdog/imx7ulp_wdt.c +6 -0
 static bool imx7ulp_wdt_is_enabled(void __iomem *base) @@ -72,7
+88,12 @@ static int imx7ulp_wdt_ping(struct watchdog_device *wdog)
+{
 	struct imx7ulp_wdt_device *wdt = watchdog_get_drvdata(wdog);
 
+	local_irq_disable();
+	writel(UNLOCK, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_ULK);
 	writel(REFRESH, wdt->base + WDOG_CNT);
+	imx7ulp_wdt_wait(wdt->base, WDOG_CS_RCS);

Per reference manual (section 59.5.4), the waits are not required here, and neither is the unlock. For practical purposes, disabling interrupts is useless as well since the refresh write operation is just a single

register write.

Quoted 4 lines

Correct, the example in reference manual does NOT have this flow for refresh, but I checked with our design team yestoday, their validation code indeed has this flow, that is why I added it for refresh operation as well.

Quoted 6 lines

If it is needed, they'll need to update the manual. Urgently. Really, missing the information that the watchdog must be unlocked in order to refresh the timer would not just be be a minor flaw in the manual. Either it is needed and must be mentioned (because the watchdog would otherwise simply not work), or it isn't needed and should not be done.

Previously, the guy I checked the refresh flow is validation guy and looks like his answer is NOT accurate, and I just checked with the SoC design owner, and he confirmed that the refresh does NOT need unlock operation.

I will drop the sequence for refresh operation in V3.

"
Hi Anson,

As we talked in IM, wdg unlock and refresh are two different operations, and they don’t impact each other. So you can refresh wdg without unlocking it. "

Thanks,
Anson