mbox series

[0/2] add ripple counter dt binding and driver

Message ID 20210226141411.2517368-1-linux@rasmusvillemoes.dk (mailing list archive)
Headers show
Series add ripple counter dt binding and driver | expand

Message

Rasmus Villemoes Feb. 26, 2021, 2:14 p.m. UTC
What I'm trying to do:

We have a board with an external watchdog circuit (the kind that is
petted by flipping a gpio). The reset output of that is split in two:
One gives an immediate interrupt, so software knows that a hard reset
is imminent. The other half removes the "reset input" from a ripple
counter, causing that to start counting at 32kHz, and after 64ms, the
SOC's reset pin then gets pulled.

Unfortunately, the board designer overlooked that the 32kHz output
from the RTC can be disabled, which (since no other component uses
it), is just what happens when clk_disable_unused() gets called. When
the watchdog fires, we do get the interrupt, but the board does not
get reset as it should, since the counter doesn't count.

So I'm thinking that the proper way to handle this is to be able to
represent that ripple counter as a clock consumer in DT and have a
driver do the clk_prepare_enable(), even if that driver doesn't and
can't do anything else. But I'm certainly open to other suggestions.

The "clk_ignore_unused" kernel parameter is fine for debugging, but
too big a hammer (since it applies to all "unused" clocks).


Rasmus Villemoes (2):
  dt-bindings: misc: add binding for generic ripple counter
  drivers: misc: add ripple counter driver

 .../devicetree/bindings/misc/ripple-ctr.txt   |  8 +++++
 drivers/misc/Kconfig                          |  7 +++++
 drivers/misc/Makefile                         |  1 +
 drivers/misc/ripple-ctr.c                     | 31 +++++++++++++++++++
 4 files changed, 47 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/misc/ripple-ctr.txt
 create mode 100644 drivers/misc/ripple-ctr.c

Comments

Arnd Bergmann Feb. 26, 2021, 2:35 p.m. UTC | #1
On Fri, Feb 26, 2021 at 3:14 PM Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:

>
> So I'm thinking that the proper way to handle this is to be able to
> represent that ripple counter as a clock consumer in DT and have a
> driver do the clk_prepare_enable(), even if that driver doesn't and
> can't do anything else. But I'm certainly open to other suggestions.

How about adding support for the optional clock to the gpio_wdt driver,
would that work?

      Arnd
Rasmus Villemoes Feb. 26, 2021, 4:35 p.m. UTC | #2
On 26/02/2021 15.35, Arnd Bergmann wrote:
> On Fri, Feb 26, 2021 at 3:14 PM Rasmus Villemoes
> <linux@rasmusvillemoes.dk> wrote:
> 
>>
>> So I'm thinking that the proper way to handle this is to be able to
>> represent that ripple counter as a clock consumer in DT and have a
>> driver do the clk_prepare_enable(), even if that driver doesn't and
>> can't do anything else. But I'm certainly open to other suggestions.
> 
> How about adding support for the optional clock to the gpio_wdt driver,
> would that work?

I think it would _work_ (all I need is some piece of code doing the
clock_prepare_enable(), and until now we've just stashed that in some
otherwise unrelated out-of-tree driver, but we're trying to get rid of
that one), but the watchdog chip isn't really the consumer of the clock
signal, so in-so-far as DT is supposed to describe the hardware, I don't
think it's appropriate.

OTOH, one could argue that the watchdog chip and the ripple counter
together constitute the watchdog circuit.

Cc += watchdog maintainers. Context: I have a gpio-wdt which can
unfortunately effectively be disabled by disabling a clock output, and
that happens automatically unless the clock has a consumer in DT. But
the actual consumer is not the gpio-wdt.
Please see
https://lore.kernel.org/lkml/20210226141411.2517368-1-linux@rasmusvillemoes.dk/
for the original thread.

Rasmus
Guenter Roeck Feb. 26, 2021, 7:53 p.m. UTC | #3
On 2/26/21 8:35 AM, Rasmus Villemoes wrote:
> On 26/02/2021 15.35, Arnd Bergmann wrote:
>> On Fri, Feb 26, 2021 at 3:14 PM Rasmus Villemoes
>> <linux@rasmusvillemoes.dk> wrote:
>>
>>>
>>> So I'm thinking that the proper way to handle this is to be able to
>>> represent that ripple counter as a clock consumer in DT and have a
>>> driver do the clk_prepare_enable(), even if that driver doesn't and
>>> can't do anything else. But I'm certainly open to other suggestions.
>>
>> How about adding support for the optional clock to the gpio_wdt driver,
>> would that work?
> 
> I think it would _work_ (all I need is some piece of code doing the
> clock_prepare_enable(), and until now we've just stashed that in some
> otherwise unrelated out-of-tree driver, but we're trying to get rid of
> that one), but the watchdog chip isn't really the consumer of the clock
> signal, so in-so-far as DT is supposed to describe the hardware, I don't
> think it's appropriate.
> 
> OTOH, one could argue that the watchdog chip and the ripple counter
> together constitute the watchdog circuit.
> 
> Cc += watchdog maintainers. Context: I have a gpio-wdt which can
> unfortunately effectively be disabled by disabling a clock output, and
> that happens automatically unless the clock has a consumer in DT. But
> the actual consumer is not the gpio-wdt.
> Please see
> https://lore.kernel.org/lkml/20210226141411.2517368-1-linux@rasmusvillemoes.dk/
> for the original thread.
> 

Sorry, I am missing something. If the watchdog is controlled by the clock,
it is a consumer of that clock. What else does "consumer" mean ? And why
not just add optional clock support to the gpio_wdt driver ?

Guenter
Rasmus Villemoes March 1, 2021, 8:34 a.m. UTC | #4
On 26/02/2021 20.53, Guenter Roeck wrote:
> On 2/26/21 8:35 AM, Rasmus Villemoes wrote:
>> On 26/02/2021 15.35, Arnd Bergmann wrote:
>>> On Fri, Feb 26, 2021 at 3:14 PM Rasmus Villemoes
>>> <linux@rasmusvillemoes.dk> wrote:
>>>
>>>>
>>>> So I'm thinking that the proper way to handle this is to be able to
>>>> represent that ripple counter as a clock consumer in DT and have a
>>>> driver do the clk_prepare_enable(), even if that driver doesn't and
>>>> can't do anything else. But I'm certainly open to other suggestions.
>>>
>>> How about adding support for the optional clock to the gpio_wdt driver,
>>> would that work?
>>
>> I think it would _work_ (all I need is some piece of code doing the
>> clock_prepare_enable(), and until now we've just stashed that in some
>> otherwise unrelated out-of-tree driver, but we're trying to get rid of
>> that one), but the watchdog chip isn't really the consumer of the clock
>> signal, so in-so-far as DT is supposed to describe the hardware, I don't
>> think it's appropriate.
>>
>> OTOH, one could argue that the watchdog chip and the ripple counter
>> together constitute the watchdog circuit.
>>
>> Cc += watchdog maintainers. Context: I have a gpio-wdt which can
>> unfortunately effectively be disabled by disabling a clock output, and
>> that happens automatically unless the clock has a consumer in DT. But
>> the actual consumer is not the gpio-wdt.
>> Please see
>> https://lore.kernel.org/lkml/20210226141411.2517368-1-linux@rasmusvillemoes.dk/
>> for the original thread.
>>
> 
> Sorry, I am missing something. If the watchdog is controlled by the clock,
> it is a consumer of that clock.

But that's just it, the watchdog chip is _not_ a consumer of the clock -
I don't think I've ever seen a gpio_wdt that is not internally clocked,
but even if they exist, that's not the case for this board.

 What else does "consumer" mean ? And why
> not just add optional clock support to the gpio_wdt driver ?

Because, the consumer is a piece of electronics sitting _between_ the
watchdog chip's reset output and the SOCs reset pin, namely the ripple
counter that implements a 64 ms delay from the watchdog fires till the
actual reset. (The watchdog's reset is also routed directly to an
interrupt; so software gets a 64 ms warning that a hard reset is imminent).

Rasmus
Arnd Bergmann March 1, 2021, 9:44 a.m. UTC | #5
On Mon, Mar 1, 2021 at 9:34 AM Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
> On 26/02/2021 20.53, Guenter Roeck wrote:
> >
> > Sorry, I am missing something. If the watchdog is controlled by the clock,
> > it is a consumer of that clock.
>
> But that's just it, the watchdog chip is _not_ a consumer of the clock -
> I don't think I've ever seen a gpio_wdt that is not internally clocked,
> but even if they exist, that's not the case for this board.
>
>  What else does "consumer" mean ? And why
> > not just add optional clock support to the gpio_wdt driver ?
>
> Because, the consumer is a piece of electronics sitting _between_ the
> watchdog chip's reset output and the SOCs reset pin, namely the ripple
> counter that implements a 64 ms delay from the watchdog fires till the
> actual reset. (The watchdog's reset is also routed directly to an
> interrupt; so software gets a 64 ms warning that a hard reset is imminent).

I think it's  a question of how you look at what the gpio_wdt device is.
While physical gpio chip is not a consumer of the clock, I agree with
Guenter that the conceptual device is: The functionality of the watchdog
in this case is provided by the combination of the external chip with the
ripple counter. I think it is therefore appropriate to have the gpio_wdt
and the driver refer to the clock as part of the watchdog.

        Arnd
Guenter Roeck March 1, 2021, 2:21 p.m. UTC | #6
On 3/1/21 1:44 AM, Arnd Bergmann wrote:
> On Mon, Mar 1, 2021 at 9:34 AM Rasmus Villemoes
> <linux@rasmusvillemoes.dk> wrote:
>> On 26/02/2021 20.53, Guenter Roeck wrote:
>>>
>>> Sorry, I am missing something. If the watchdog is controlled by the clock,
>>> it is a consumer of that clock.
>>
>> But that's just it, the watchdog chip is _not_ a consumer of the clock -
>> I don't think I've ever seen a gpio_wdt that is not internally clocked,
>> but even if they exist, that's not the case for this board.
>>
>>  What else does "consumer" mean ? And why
>>> not just add optional clock support to the gpio_wdt driver ?
>>
>> Because, the consumer is a piece of electronics sitting _between_ the
>> watchdog chip's reset output and the SOCs reset pin, namely the ripple
>> counter that implements a 64 ms delay from the watchdog fires till the
>> actual reset. (The watchdog's reset is also routed directly to an
>> interrupt; so software gets a 64 ms warning that a hard reset is imminent).
> 
> I think it's  a question of how you look at what the gpio_wdt device is.
> While physical gpio chip is not a consumer of the clock, I agree with
> Guenter that the conceptual device is: The functionality of the watchdog
> in this case is provided by the combination of the external chip with the
> ripple counter. I think it is therefore appropriate to have the gpio_wdt
> and the driver refer to the clock as part of the watchdog.
> 

I agree. All electronics needed for the watchdog to operate is part of the
watchdog, and for me that includes the circuitry that connects it to the
reset pin. The clock is needed for proper watchdog operation, so I would
consider the watchdog to be a consumer.

Guenter
Rasmus Villemoes March 4, 2021, 10:12 p.m. UTC | #7
As Arnd and Guenther suggested, this adds support to the gpio_wdt
driver for being a consumer of the clock driving the ripple
counter. However, I don't think it should be merged as-is, see below.

The first patch makes sense on its own, quick grepping suggests plenty
of places that could benefit from this, and I thought it would be odd
to have to re-introduce a .remove callback in the gpio_wdt driver.

Unfortunately, this turns out to be a bit of an "operation succeeded,
patient (almost) died": We use CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
because the watchdog has a rather short timeout (1.0-2.25s, 1.6s
typical according to data sheet). At first, I put the new code right
after the devm_gpiod_get(), but the problem is that this early, we get
-EPROBE_DEFER since the clock provider (the RTC which sits off i2c)
isn't probed yet. But then the board would reset because it takes way
too long for the rest of the machine to initialize. [The bootloader
makes sure to turn on the RTC's clock output so the watchdog is
actually functional, the task here is to figure out the proper way to
prevent clk_disable_unused() from disabling it.]

Moving the logic to after the first "is it always-running and if so
give it an initial ping" made the board survive, but unfortunately the
second, and succesful, probe happens a little more than a second
later, which happens to work on this particular board, but is
obviously not suitable for production given that it's already above
what the spec says, and other random changes in the future might make
the gap even wider.

So I don't know. The hardware is obviously misdesigned, and I don't
know how far the mainline kernel should stretch to support this; OTOH
the kernel does contain lots of workarounds for quirks and hardware
bugs. 




Rasmus Villemoes (3):
  clk: add devm_clk_prepare_enable() helper
  dt-bindings: watchdog: add optional "delay" clock to gpio-wdt binding
  watchdog: gpio_wdt: implement support for optional "delay" clock

 .../devicetree/bindings/watchdog/gpio-wdt.txt |  6 ++++
 .../driver-api/driver-model/devres.rst        |  1 +
 drivers/clk/clk-devres.c                      | 29 +++++++++++++++++++
 drivers/watchdog/gpio_wdt.c                   |  9 ++++++
 include/linux/clk.h                           | 13 +++++++++
 5 files changed, 58 insertions(+)
Guenter Roeck March 9, 2021, 5:51 a.m. UTC | #8
On 3/4/21 2:12 PM, Rasmus Villemoes wrote:
> As Arnd and Guenther suggested, this adds support to the gpio_wdt
> driver for being a consumer of the clock driving the ripple
> counter. However, I don't think it should be merged as-is, see below.
> 
> The first patch makes sense on its own, quick grepping suggests plenty
> of places that could benefit from this, and I thought it would be odd
> to have to re-introduce a .remove callback in the gpio_wdt driver.
> 

This has zero chance to be accepted. As suggested in the patch,
just use devm_add_action(), like many other watchdog drivers.

> Unfortunately, this turns out to be a bit of an "operation succeeded,
> patient (almost) died": We use CONFIG_GPIO_WATCHDOG_ARCH_INITCALL
> because the watchdog has a rather short timeout (1.0-2.25s, 1.6s
> typical according to data sheet). At first, I put the new code right
> after the devm_gpiod_get(), but the problem is that this early, we get
> -EPROBE_DEFER since the clock provider (the RTC which sits off i2c)
> isn't probed yet. But then the board would reset because it takes way
> too long for the rest of the machine to initialize. [The bootloader
> makes sure to turn on the RTC's clock output so the watchdog is
> actually functional, the task here is to figure out the proper way to
> prevent clk_disable_unused() from disabling it.]
> 

Is there a property indicating always-on for clocks, similar to
regulator-always-on ? The idea seems to exist, but it looks like
it is always explict (ie mentioned somewhere in the code that a clock
is always on, or "safe"). It would help if the clock in question
can be marked as always-on without explicit consumer.

Thanks,
Guenter

> Moving the logic to after the first "is it always-running and if so
> give it an initial ping" made the board survive, but unfortunately the
> second, and succesful, probe happens a little more than a second
> later, which happens to work on this particular board, but is
> obviously not suitable for production given that it's already above
> what the spec says, and other random changes in the future might make
> the gap even wider.
> 
> So I don't know. The hardware is obviously misdesigned, and I don't
> know how far the mainline kernel should stretch to support this; OTOH
> the kernel does contain lots of workarounds for quirks and hardware
> bugs. 
> 
> 
> 
> 
> Rasmus Villemoes (3):
>   clk: add devm_clk_prepare_enable() helper
>   dt-bindings: watchdog: add optional "delay" clock to gpio-wdt binding
>   watchdog: gpio_wdt: implement support for optional "delay" clock
> 
>  .../devicetree/bindings/watchdog/gpio-wdt.txt |  6 ++++
>  .../driver-api/driver-model/devres.rst        |  1 +
>  drivers/clk/clk-devres.c                      | 29 +++++++++++++++++++
>  drivers/watchdog/gpio_wdt.c                   |  9 ++++++
>  include/linux/clk.h                           | 13 +++++++++
>  5 files changed, 58 insertions(+)
>