How to keep watchdog enabled?

This forum is for users of Microchip MPUs and who are interested in using Linux OS.

Moderator: nferre

pbugalski
Posts: 23
Joined: Thu Nov 29, 2018 4:28 pm

How to keep watchdog enabled?

Wed Apr 29, 2020 8:18 am

Hi,

I have a problem with watchdog on SAMA5D28. I'm using custom board, but the same problem appears on SAMA5D2-Xplained and SAMA5D27-SOM1-EK1.
Customer requirement is to have watchdog enabled all the time to avoid system hanging. Watchdog is enabled in very early bootloader phase, but Linux kernel seems to disable watchdog during initialization.
I'm using mainline kernel, but I also tested on linux-at91 and the same behaviour exists - watchdog is enabled by bootloader, but when Linux boots it seems to be disabled.
So my question is: how can I keep watchdog enabled?

Best Regards,
Piotr
blue_z
Location: USA
Posts: 2154
Joined: Thu Apr 19, 2007 10:15 pm

Re: How to keep watchdog enabled?

Wed Apr 29, 2020 10:58 pm

pbugalski wrote: ...but Linux kernel seems to disable watchdog during initialization.
...
but when Linux boots it seems to be disabled.
Why are you so unsure?
Have you determined whether the WDT is disabled or not?


pbugalski wrote: So my question is: how can I keep watchdog enabled?
You would need to redefine the functionality of the Linux watchdog timer device, and hack the kernel code.

The Linux watchdog timer is (essentially) only active when the /dev/watchdog device is open by a userspace program. Normally the watchdog timer is disabled/inactive when the /dev/watchdog device is closed.
This is documented in the Documentation/watchdog/watchdog-api.rst file in the kernel source.
IOW /dev/watchdog is controlled by a userspace program, and is not (originally) intended as a system-wide monitor/failsafe-mechanism.

Regards
pbugalski
Posts: 23
Joined: Thu Nov 29, 2018 4:28 pm

Re: How to keep watchdog enabled?

Wed Apr 29, 2020 11:43 pm

blue_z wrote:
pbugalski wrote: ...but Linux kernel seems to disable watchdog during initialization.
...
but when Linux boots it seems to be disabled.
Why are you so unsure?
Have you determined whether the WDT is disabled or not?
Now I'm sure, kernel disables watchdog during boot.
blue_z wrote:
pbugalski wrote: So my question is: how can I keep watchdog enabled?
You would need to redefine the functionality of the Linux watchdog timer device, and hack the kernel code.

The Linux watchdog timer is (essentially) only active when the /dev/watchdog device is open by a userspace program. Normally the watchdog timer is disabled/inactive when the /dev/watchdog device is closed.
Sama5d4 watchdog driver supports magic close feature so fortunately it will not be disabled on close by default.

Anyway my problem is not with closing, but system startup. If anything goes wrong I need watchdog to reset the device. If some error occurs, boot may not even reach userspace code. Please have a look at comment of this patch: https://github.com/torvalds/linux/commi ... 31caac53e1
It looks like watchdog framework can support watchdog started before user code opens /dev/watchdog device, so the question is how to use it?

If patching kernel is the only option it's also fine, but I wanted to be sure there is no other solution.

Best Regards,
Piotr
blue_z
Location: USA
Posts: 2154
Joined: Thu Apr 19, 2007 10:15 pm

Re: How to keep watchdog enabled?

Thu Apr 30, 2020 7:44 pm

pbugalski wrote: Please have a look at comment of this patch: https://github.com/torvalds/linux/commi ... 31caac53e1
It looks like watchdog framework can support watchdog started before user code opens /dev/watchdog device, so the question is how to use it?
Since you're using the mainline kernel, then that patch should already be installed.
By default CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED is enabled.
Either set the open-timeout using the `menuconfig`, or as a parameter ("watchdog.open_timeout=N") in the kernel command line (e.g. in U-Boot's bootargs variable).

pbugalski wrote: If patching kernel is the only option it's also fine, but I wanted to be sure there is no other solution.
Looks like sama5d4_wdt.c (as well as many other watchdog drivers) have not been updated to support CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED and that open-timeout. I see at least two obvious changes that need to be made to sama5d4_wdt.c:

1. the status element in the struct watchdog_device needs to be maintained by the driver, in particular the WDOG_HW_RUNNING bit so that function watchdog_hw_running() returns accurate results.
Note that a handful of drivers do utilize the WDOG_HW_RUNNING bit, but the majority do not (?!!!).

2. Assuming that the WDT is being disabled by the Linux driver, then the driver needs to be made compatible with these features, and keep the WDT enabled. The suspect code appears to be the `if (!wdt_enabled) { ... }` block of sama5d4_wdt_init().
Note that during this probe phase `wdt->mr` does not contain or match the actual HW register value, but is an initialized value from of_sama5d4_wdt_init(). Since the WDT mode register is readable, I suspect that the hard assignment of `wdt->mr = AT91_WDT_WDDIS;` in of_sama5d4_wdt_init() needs to be replaced with the actual value of the AT91_WDT_WDDIS mode bit.


Regards
pbugalski
Posts: 23
Joined: Thu Nov 29, 2018 4:28 pm

Re: How to keep watchdog enabled?

Thu Apr 30, 2020 9:56 pm

blue_z wrote:
pbugalski wrote: Please have a look at comment of this patch: https://github.com/torvalds/linux/commi ... 31caac53e1
It looks like watchdog framework can support watchdog started before user code opens /dev/watchdog device, so the question is how to use it?
Since you're using the mainline kernel, then that patch should already be installed.
By default CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED is enabled.
Either set the open-timeout using the `menuconfig`, or as a parameter ("watchdog.open_timeout=N") in the kernel command line (e.g. in U-Boot's bootargs variable).
In fact I'm using 5.0 kernel from mainline, but it doesn't matter. Neither linux-at91 nor newest mainline works as expected for embedded solution. I pointed to this commit just to show description and rationalization how watchdog should work. Setting open-timeout will not enable watchdog, it still will be disabled during kernel boot.
blue_z wrote:
pbugalski wrote: If patching kernel is the only option it's also fine, but I wanted to be sure there is no other solution.
Looks like sama5d4_wdt.c (as well as many other watchdog drivers) have not been updated to support CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED and that open-timeout. I see at least two obvious changes that need to be made to sama5d4_wdt.c:

1. the status element in the struct watchdog_device needs to be maintained by the driver, in particular the WDOG_HW_RUNNING bit so that function watchdog_hw_running() returns accurate results.
Note that a handful of drivers do utilize the WDOG_HW_RUNNING bit, but the majority do not (?!!!).

2. Assuming that the WDT is being disabled by the Linux driver, then the driver needs to be made compatible with these features, and keep the WDT enabled. The suspect code appears to be the `if (!wdt_enabled) { ... }` block of sama5d4_wdt_init().
Note that during this probe phase `wdt->mr` does not contain or match the actual HW register value, but is an initialized value from of_sama5d4_wdt_init(). Since the WDT mode register is readable, I suspect that the hard assignment of `wdt->mr = AT91_WDT_WDDIS;` in of_sama5d4_wdt_init() needs to be replaced with the actual value of the AT91_WDT_WDDIS mode bit.
I know about that, I have working patch to sama5d4_wdt driver, but I wanted to avoid changes in kernel. If it's possible to keep watchdog enabled without changing kernel code, please let me know. Otherwise I will patch Atmel's driver, but I still hope there is a way to use it without changes.

Regards,
Piotr

Return to “LINUX”

Who is online

Users browsing this forum: No registered users and 11 guests