2020-10-26 09:38:03

by Chunyan Zhang

[permalink] [raw]
Subject: [PATCH 0/3] A few fixes to sprd watchdog driver

From: Chunyan Zhang <[email protected]>

A few issues about sprd watchdog driver were found recently, this
patchset would fix them.

Lingling Xu (3):
watchdog: sprd: should not disable watchdog in resume
watchdog: sprd: change timeout value from 1000 to 2000
watchdog: sprd: check busy bit before kick watchdog

drivers/watchdog/sprd_wdt.c | 38 +++++++++++++++++--------------------
1 file changed, 17 insertions(+), 21 deletions(-)

--
2.20.1


2020-10-26 09:38:57

by Chunyan Zhang

[permalink] [raw]
Subject: [PATCH 2/3] watchdog: sprd: change timeout value from 1000 to 2000

From: Lingling Xu <[email protected]>

Because cpu_relax() takes different time on different SoCs, for some rare
cases, it would take more than 1000 cycles for waitting load operation
finished. The result of many times testing verified that changing the
timeout value to 2000 can solve the issue.

Fixes: 477603467009 ("watchdog: Add Spreadtrum watchdog driver")
Signed-off-by: Lingling Xu <[email protected]>
Signed-off-by: Chunyan Zhang <[email protected]>
---
drivers/watchdog/sprd_wdt.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
index f3c90b4afead..4f2a8c6d6485 100644
--- a/drivers/watchdog/sprd_wdt.c
+++ b/drivers/watchdog/sprd_wdt.c
@@ -53,7 +53,7 @@

#define SPRD_WDT_CNT_HIGH_SHIFT 16
#define SPRD_WDT_LOW_VALUE_MASK GENMASK(15, 0)
-#define SPRD_WDT_LOAD_TIMEOUT 1000
+#define SPRD_WDT_LOAD_TIMEOUT 2000

struct sprd_wdt {
void __iomem *base;
--
2.20.1

2020-10-26 09:40:00

by Chunyan Zhang

[permalink] [raw]
Subject: [PATCH 3/3] watchdog: sprd: check busy bit before kick watchdog

From: Lingling Xu <[email protected]>

As the specification described, checking busy bit must be done before kick
watchdog.

Fixes: 477603467009 ("watchdog: Add Spreadtrum watchdog driver")
Signed-off-by: Lingling Xu <[email protected]>
Signed-off-by: Chunyan Zhang <[email protected]>
---
drivers/watchdog/sprd_wdt.c | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
index 4f2a8c6d6485..14071c66ff49 100644
--- a/drivers/watchdog/sprd_wdt.c
+++ b/drivers/watchdog/sprd_wdt.c
@@ -108,20 +108,8 @@ static int sprd_wdt_load_value(struct sprd_wdt *wdt, u32 timeout,
u32 tmr_step = timeout * SPRD_WDT_CNT_STEP;
u32 prtmr_step = pretimeout * SPRD_WDT_CNT_STEP;

- sprd_wdt_unlock(wdt->base);
- writel_relaxed((tmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
- SPRD_WDT_LOW_VALUE_MASK, wdt->base + SPRD_WDT_LOAD_HIGH);
- writel_relaxed((tmr_step & SPRD_WDT_LOW_VALUE_MASK),
- wdt->base + SPRD_WDT_LOAD_LOW);
- writel_relaxed((prtmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
- SPRD_WDT_LOW_VALUE_MASK,
- wdt->base + SPRD_WDT_IRQ_LOAD_HIGH);
- writel_relaxed(prtmr_step & SPRD_WDT_LOW_VALUE_MASK,
- wdt->base + SPRD_WDT_IRQ_LOAD_LOW);
- sprd_wdt_lock(wdt->base);
-
/*
- * Waiting the load value operation done,
+ * Waiting the last load value operation done,
* it needs two or three RTC clock cycles.
*/
do {
@@ -134,6 +122,19 @@ static int sprd_wdt_load_value(struct sprd_wdt *wdt, u32 timeout,

if (delay_cnt >= SPRD_WDT_LOAD_TIMEOUT)
return -EBUSY;
+
+ sprd_wdt_unlock(wdt->base);
+ writel_relaxed((tmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
+ SPRD_WDT_LOW_VALUE_MASK, wdt->base + SPRD_WDT_LOAD_HIGH);
+ writel_relaxed((tmr_step & SPRD_WDT_LOW_VALUE_MASK),
+ wdt->base + SPRD_WDT_LOAD_LOW);
+ writel_relaxed((prtmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
+ SPRD_WDT_LOW_VALUE_MASK,
+ wdt->base + SPRD_WDT_IRQ_LOAD_HIGH);
+ writel_relaxed(prtmr_step & SPRD_WDT_LOW_VALUE_MASK,
+ wdt->base + SPRD_WDT_IRQ_LOAD_LOW);
+ sprd_wdt_lock(wdt->base);
+
return 0;
}

--
2.20.1

2020-10-26 17:56:14

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH 2/3] watchdog: sprd: change timeout value from 1000 to 2000

On 10/26/20 1:09 AM, Chunyan Zhang wrote:
> From: Lingling Xu <[email protected]>
>
> Because cpu_relax() takes different time on different SoCs, for some rare
> cases, it would take more than 1000 cycles for waitting load operation

waiting

> finished. The result of many times testing verified that changing the
> timeout value to 2000 can solve the issue.
>

This is just a kludge that doesn't address the underlying problem.
As the wait loop states, "Waiting the load value operation done,
it needs two or three RTC clock cycles". This means the loop
should wait for a maximum number of clock cycles, and not run
as hot loop. If we assume that clk_get_rate() returns the clock
frequency, that frequency can be used to determine how long this
needs to be retried. It might also make sense - depending on how
long this actually takes - to use usleep_range() instead of
cpu_relax() to avoid the hot loop.

Guenter

> Fixes: 477603467009 ("watchdog: Add Spreadtrum watchdog driver")
> Signed-off-by: Lingling Xu <[email protected]>
> Signed-off-by: Chunyan Zhang <[email protected]>
> ---
> drivers/watchdog/sprd_wdt.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
> index f3c90b4afead..4f2a8c6d6485 100644
> --- a/drivers/watchdog/sprd_wdt.c
> +++ b/drivers/watchdog/sprd_wdt.c
> @@ -53,7 +53,7 @@
>
> #define SPRD_WDT_CNT_HIGH_SHIFT 16
> #define SPRD_WDT_LOW_VALUE_MASK GENMASK(15, 0)
> -#define SPRD_WDT_LOAD_TIMEOUT 1000
> +#define SPRD_WDT_LOAD_TIMEOUT 2000
>
> struct sprd_wdt {
> void __iomem *base;
>

2020-10-26 18:54:36

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH 3/3] watchdog: sprd: check busy bit before kick watchdog

On 10/26/20 1:09 AM, Chunyan Zhang wrote:
> From: Lingling Xu <[email protected]>
>
> As the specification described, checking busy bit must be done before kick
> watchdog.
>

That is a key functional change: So far the code checked if a value
was accepted after loading it. That is no longer the case. Effectively,
with this change, the _next_ operation will now check if the previous
operation was accepted. Is this intentional ?

Also, does this really solve a problem, or is it just an optimization ?
By checking for busy prior to an operation instead of after it the only
real difference is that the busy check will most likely succeed immediately
because enough time has passed since the last write.

Ultimately it is your call how you want to handle this, but I think the
impact should be spelled out.

Guenter

> Fixes: 477603467009 ("watchdog: Add Spreadtrum watchdog driver")
> Signed-off-by: Lingling Xu <[email protected]>
> Signed-off-by: Chunyan Zhang <[email protected]>
> ---
> drivers/watchdog/sprd_wdt.c | 27 ++++++++++++++-------------
> 1 file changed, 14 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
> index 4f2a8c6d6485..14071c66ff49 100644
> --- a/drivers/watchdog/sprd_wdt.c
> +++ b/drivers/watchdog/sprd_wdt.c
> @@ -108,20 +108,8 @@ static int sprd_wdt_load_value(struct sprd_wdt *wdt, u32 timeout,
> u32 tmr_step = timeout * SPRD_WDT_CNT_STEP;
> u32 prtmr_step = pretimeout * SPRD_WDT_CNT_STEP;
>
> - sprd_wdt_unlock(wdt->base);
> - writel_relaxed((tmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> - SPRD_WDT_LOW_VALUE_MASK, wdt->base + SPRD_WDT_LOAD_HIGH);
> - writel_relaxed((tmr_step & SPRD_WDT_LOW_VALUE_MASK),
> - wdt->base + SPRD_WDT_LOAD_LOW);
> - writel_relaxed((prtmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> - SPRD_WDT_LOW_VALUE_MASK,
> - wdt->base + SPRD_WDT_IRQ_LOAD_HIGH);
> - writel_relaxed(prtmr_step & SPRD_WDT_LOW_VALUE_MASK,
> - wdt->base + SPRD_WDT_IRQ_LOAD_LOW);
> - sprd_wdt_lock(wdt->base);
> -
> /*
> - * Waiting the load value operation done,
> + * Waiting the last load value operation done,
> * it needs two or three RTC clock cycles.
> */
> do {
> @@ -134,6 +122,19 @@ static int sprd_wdt_load_value(struct sprd_wdt *wdt, u32 timeout,
>
> if (delay_cnt >= SPRD_WDT_LOAD_TIMEOUT)
> return -EBUSY;
> +
> + sprd_wdt_unlock(wdt->base);
> + writel_relaxed((tmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> + SPRD_WDT_LOW_VALUE_MASK, wdt->base + SPRD_WDT_LOAD_HIGH);
> + writel_relaxed((tmr_step & SPRD_WDT_LOW_VALUE_MASK),
> + wdt->base + SPRD_WDT_LOAD_LOW);
> + writel_relaxed((prtmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> + SPRD_WDT_LOW_VALUE_MASK,
> + wdt->base + SPRD_WDT_IRQ_LOAD_HIGH);
> + writel_relaxed(prtmr_step & SPRD_WDT_LOW_VALUE_MASK,
> + wdt->base + SPRD_WDT_IRQ_LOAD_LOW);
> + sprd_wdt_lock(wdt->base);
> +
> return 0;
> }
>
>

2020-10-27 14:25:55

by Chunyan Zhang

[permalink] [raw]
Subject: Re: [PATCH 2/3] watchdog: sprd: change timeout value from 1000 to 2000

On Mon, 26 Oct 2020 at 22:36, Guenter Roeck <[email protected]> wrote:
>
> On 10/26/20 1:09 AM, Chunyan Zhang wrote:
> > From: Lingling Xu <[email protected]>
> >
> > Because cpu_relax() takes different time on different SoCs, for some rare
> > cases, it would take more than 1000 cycles for waitting load operation
>
> waiting

Ok.

>
> > finished. The result of many times testing verified that changing the
> > timeout value to 2000 can solve the issue.
> >
>
> This is just a kludge that doesn't address the underlying problem.
> As the wait loop states, "Waiting the load value operation done,
> it needs two or three RTC clock cycles". This means the loop
> should wait for a maximum number of clock cycles, and not run
> as hot loop. If we assume that clk_get_rate() returns the clock
> frequency, that frequency can be used to determine how long this
> needs to be retried. It might also make sense - depending on how
> long this actually takes - to use usleep_range() instead of
> cpu_relax() to avoid the hot loop.

Agree, using usleep_range() instead makes more sense, I will look into that.

Thanks for your review.

Chunyan

>
> Guenter
>
> > Fixes: 477603467009 ("watchdog: Add Spreadtrum watchdog driver")
> > Signed-off-by: Lingling Xu <[email protected]>
> > Signed-off-by: Chunyan Zhang <[email protected]>
> > ---
> > drivers/watchdog/sprd_wdt.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
> > index f3c90b4afead..4f2a8c6d6485 100644
> > --- a/drivers/watchdog/sprd_wdt.c
> > +++ b/drivers/watchdog/sprd_wdt.c
> > @@ -53,7 +53,7 @@
> >
> > #define SPRD_WDT_CNT_HIGH_SHIFT 16
> > #define SPRD_WDT_LOW_VALUE_MASK GENMASK(15, 0)
> > -#define SPRD_WDT_LOAD_TIMEOUT 1000
> > +#define SPRD_WDT_LOAD_TIMEOUT 2000
> >
> > struct sprd_wdt {
> > void __iomem *base;
> >
>

2020-10-28 06:10:57

by Chunyan Zhang

[permalink] [raw]
Subject: Re: [PATCH 3/3] watchdog: sprd: check busy bit before kick watchdog

On Mon, 26 Oct 2020 at 22:44, Guenter Roeck <[email protected]> wrote:
>
> On 10/26/20 1:09 AM, Chunyan Zhang wrote:
> > From: Lingling Xu <[email protected]>
> >
> > As the specification described, checking busy bit must be done before kick
> > watchdog.
> >
>
> That is a key functional change: So far the code checked if a value
> was accepted after loading it. That is no longer the case. Effectively,
> with this change, the _next_ operation will now check if the previous
> operation was accepted. Is this intentional ?

Yes, the busy bit indicates whether the previous operation is done, so
we have to make sure the last loading completed (the busy bit is not
set) before new loading.

The spec says that this bit is set after a new loading, and would last
2 or 3 RTC clock cycles.

>
> Also, does this really solve a problem, or is it just an optimization ?
> By checking for busy prior to an operation instead of after it the only
> real difference is that the busy check will most likely succeed immediately
> because enough time has passed since the last write.
>
> Ultimately it is your call how you want to handle this, but I think the
> impact should be spelled out.

Ok, I will add more details in the commit message.

Many thanks for the review!

Chunyan

>
> Guenter
>
> > Fixes: 477603467009 ("watchdog: Add Spreadtrum watchdog driver")
> > Signed-off-by: Lingling Xu <[email protected]>
> > Signed-off-by: Chunyan Zhang <[email protected]>
> > ---
> > drivers/watchdog/sprd_wdt.c | 27 ++++++++++++++-------------
> > 1 file changed, 14 insertions(+), 13 deletions(-)
> >
> > diff --git a/drivers/watchdog/sprd_wdt.c b/drivers/watchdog/sprd_wdt.c
> > index 4f2a8c6d6485..14071c66ff49 100644
> > --- a/drivers/watchdog/sprd_wdt.c
> > +++ b/drivers/watchdog/sprd_wdt.c
> > @@ -108,20 +108,8 @@ static int sprd_wdt_load_value(struct sprd_wdt *wdt, u32 timeout,
> > u32 tmr_step = timeout * SPRD_WDT_CNT_STEP;
> > u32 prtmr_step = pretimeout * SPRD_WDT_CNT_STEP;
> >
> > - sprd_wdt_unlock(wdt->base);
> > - writel_relaxed((tmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> > - SPRD_WDT_LOW_VALUE_MASK, wdt->base + SPRD_WDT_LOAD_HIGH);
> > - writel_relaxed((tmr_step & SPRD_WDT_LOW_VALUE_MASK),
> > - wdt->base + SPRD_WDT_LOAD_LOW);
> > - writel_relaxed((prtmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> > - SPRD_WDT_LOW_VALUE_MASK,
> > - wdt->base + SPRD_WDT_IRQ_LOAD_HIGH);
> > - writel_relaxed(prtmr_step & SPRD_WDT_LOW_VALUE_MASK,
> > - wdt->base + SPRD_WDT_IRQ_LOAD_LOW);
> > - sprd_wdt_lock(wdt->base);
> > -
> > /*
> > - * Waiting the load value operation done,
> > + * Waiting the last load value operation done,
> > * it needs two or three RTC clock cycles.
> > */
> > do {
> > @@ -134,6 +122,19 @@ static int sprd_wdt_load_value(struct sprd_wdt *wdt, u32 timeout,
> >
> > if (delay_cnt >= SPRD_WDT_LOAD_TIMEOUT)
> > return -EBUSY;
> > +
> > + sprd_wdt_unlock(wdt->base);
> > + writel_relaxed((tmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> > + SPRD_WDT_LOW_VALUE_MASK, wdt->base + SPRD_WDT_LOAD_HIGH);
> > + writel_relaxed((tmr_step & SPRD_WDT_LOW_VALUE_MASK),
> > + wdt->base + SPRD_WDT_LOAD_LOW);
> > + writel_relaxed((prtmr_step >> SPRD_WDT_CNT_HIGH_SHIFT) &
> > + SPRD_WDT_LOW_VALUE_MASK,
> > + wdt->base + SPRD_WDT_IRQ_LOAD_HIGH);
> > + writel_relaxed(prtmr_step & SPRD_WDT_LOW_VALUE_MASK,
> > + wdt->base + SPRD_WDT_IRQ_LOAD_LOW);
> > + sprd_wdt_lock(wdt->base);
> > +
> > return 0;
> > }
> >
> >
>