1 // SPDX-License-Identifier: GPL-2.0-only
3 * sl28cpld watchdog driver
5 * Copyright 2020 Kontron Europe GmbH
8 #include <linux/kernel.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13 #include <linux/regmap.h>
14 #include <linux/watchdog.h>
17 * Watchdog timer block registers.
20 #define WDT_CTRL_EN BIT(0)
21 #define WDT_CTRL_LOCK BIT(2)
22 #define WDT_CTRL_ASSERT_SYS_RESET BIT(6)
23 #define WDT_CTRL_ASSERT_WDT_TIMEOUT BIT(7)
24 #define WDT_TIMEOUT 0x01
26 #define WDT_KICK_VALUE 0x6b
27 #define WDT_COUNT 0x03
29 #define WDT_DEFAULT_TIMEOUT 10
31 static bool nowayout = WATCHDOG_NOWAYOUT;
32 module_param(nowayout, bool, 0);
33 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
34 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
37 module_param(timeout, int, 0);
38 MODULE_PARM_DESC(timeout, "Initial watchdog timeout in seconds");
41 struct watchdog_device wdd;
42 struct regmap *regmap;
44 bool assert_wdt_timeout;
47 static int sl28cpld_wdt_ping(struct watchdog_device *wdd)
49 struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
51 return regmap_write(wdt->regmap, wdt->offset + WDT_KICK,
55 static int sl28cpld_wdt_start(struct watchdog_device *wdd)
57 struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
60 val = WDT_CTRL_EN | WDT_CTRL_ASSERT_SYS_RESET;
61 if (wdt->assert_wdt_timeout)
62 val |= WDT_CTRL_ASSERT_WDT_TIMEOUT;
66 return regmap_update_bits(wdt->regmap, wdt->offset + WDT_CTRL,
70 static int sl28cpld_wdt_stop(struct watchdog_device *wdd)
72 struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
74 return regmap_update_bits(wdt->regmap, wdt->offset + WDT_CTRL,
78 static unsigned int sl28cpld_wdt_get_timeleft(struct watchdog_device *wdd)
80 struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
84 ret = regmap_read(wdt->regmap, wdt->offset + WDT_COUNT, &val);
91 static int sl28cpld_wdt_set_timeout(struct watchdog_device *wdd,
94 struct sl28cpld_wdt *wdt = watchdog_get_drvdata(wdd);
97 ret = regmap_write(wdt->regmap, wdt->offset + WDT_TIMEOUT, timeout);
101 wdd->timeout = timeout;
106 static const struct watchdog_info sl28cpld_wdt_info = {
107 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
108 .identity = "sl28cpld watchdog",
111 static const struct watchdog_ops sl28cpld_wdt_ops = {
112 .owner = THIS_MODULE,
113 .start = sl28cpld_wdt_start,
114 .stop = sl28cpld_wdt_stop,
115 .ping = sl28cpld_wdt_ping,
116 .set_timeout = sl28cpld_wdt_set_timeout,
117 .get_timeleft = sl28cpld_wdt_get_timeleft,
120 static int sl28cpld_wdt_probe(struct platform_device *pdev)
122 struct watchdog_device *wdd;
123 struct sl28cpld_wdt *wdt;
128 if (!pdev->dev.parent)
131 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
135 wdt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
139 ret = device_property_read_u32(&pdev->dev, "reg", &wdt->offset);
143 wdt->assert_wdt_timeout = device_property_read_bool(&pdev->dev,
144 "kontron,assert-wdt-timeout-pin");
146 /* initialize struct watchdog_device */
148 wdd->parent = &pdev->dev;
149 wdd->info = &sl28cpld_wdt_info;
150 wdd->ops = &sl28cpld_wdt_ops;
151 wdd->min_timeout = 1;
152 wdd->max_timeout = 255;
154 watchdog_set_drvdata(wdd, wdt);
155 watchdog_stop_on_reboot(wdd);
158 * Read the status early, in case of an error, we haven't modified the
161 ret = regmap_read(wdt->regmap, wdt->offset + WDT_CTRL, &status);
166 * Initial timeout value, may be overwritten by device tree or module
167 * parameter in watchdog_init_timeout().
169 * Reading a zero here means that either the hardware has a default
170 * value of zero (which is very unlikely and definitely a hardware
171 * bug) or the bootloader set it to zero. In any case, we handle
172 * this case gracefully and set out own timeout.
174 ret = regmap_read(wdt->regmap, wdt->offset + WDT_TIMEOUT, &val);
181 wdd->timeout = WDT_DEFAULT_TIMEOUT;
183 watchdog_init_timeout(wdd, timeout, &pdev->dev);
184 sl28cpld_wdt_set_timeout(wdd, wdd->timeout);
186 /* if the watchdog is locked, we set nowayout */
187 if (status & WDT_CTRL_LOCK)
189 watchdog_set_nowayout(wdd, nowayout);
192 * If watchdog is already running, keep it enabled, but make
193 * sure its mode is set correctly.
195 if (status & WDT_CTRL_EN) {
196 sl28cpld_wdt_start(wdd);
197 set_bit(WDOG_HW_RUNNING, &wdd->status);
200 ret = devm_watchdog_register_device(&pdev->dev, wdd);
202 dev_err(&pdev->dev, "failed to register watchdog device\n");
206 dev_info(&pdev->dev, "initial timeout %d sec%s\n",
207 wdd->timeout, nowayout ? ", nowayout" : "");
212 static const struct of_device_id sl28cpld_wdt_of_match[] = {
213 { .compatible = "kontron,sl28cpld-wdt" },
216 MODULE_DEVICE_TABLE(of, sl28cpld_wdt_of_match);
218 static struct platform_driver sl28cpld_wdt_driver = {
219 .probe = sl28cpld_wdt_probe,
221 .name = "sl28cpld-wdt",
222 .of_match_table = sl28cpld_wdt_of_match,
225 module_platform_driver(sl28cpld_wdt_driver);
227 MODULE_DESCRIPTION("sl28cpld Watchdog Driver");
228 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
229 MODULE_LICENSE("GPL");