1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
4 * SMP86xx/SMP87xx Watchdog driver
7 #include <linux/bitops.h>
9 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/platform_device.h>
15 #include <linux/watchdog.h>
17 #define DEFAULT_TIMEOUT 30
19 static bool nowayout = WATCHDOG_NOWAYOUT;
20 module_param(nowayout, bool, 0);
21 MODULE_PARM_DESC(nowayout,
22 "Watchdog cannot be stopped once started (default="
23 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
25 static unsigned int timeout;
26 module_param(timeout, int, 0);
27 MODULE_PARM_DESC(timeout, "Watchdog timeout");
30 * Counter counts down from programmed value. Reset asserts when
31 * the counter reaches 1.
36 #define WD_CONFIG_XTAL_IN BIT(0)
37 #define WD_CONFIG_DISABLE BIT(31)
39 struct tangox_wdt_device {
40 struct watchdog_device wdt;
42 unsigned long clk_rate;
46 static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
47 unsigned int new_timeout)
49 wdt->timeout = new_timeout;
54 static int tangox_wdt_start(struct watchdog_device *wdt)
56 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
59 ticks = 1 + wdt->timeout * dev->clk_rate;
60 writel(ticks, dev->base + WD_COUNTER);
65 static int tangox_wdt_stop(struct watchdog_device *wdt)
67 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
69 writel(0, dev->base + WD_COUNTER);
74 static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
76 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
79 count = readl(dev->base + WD_COUNTER);
84 return (count - 1) / dev->clk_rate;
87 static const struct watchdog_info tangox_wdt_info = {
88 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
89 .identity = "tangox watchdog",
92 static int tangox_wdt_restart(struct watchdog_device *wdt,
93 unsigned long action, void *data)
95 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
97 writel(1, dev->base + WD_COUNTER);
102 static const struct watchdog_ops tangox_wdt_ops = {
103 .start = tangox_wdt_start,
104 .stop = tangox_wdt_stop,
105 .set_timeout = tangox_wdt_set_timeout,
106 .get_timeleft = tangox_wdt_get_timeleft,
107 .restart = tangox_wdt_restart,
110 static int tangox_wdt_probe(struct platform_device *pdev)
112 struct tangox_wdt_device *dev;
113 struct resource *res;
117 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 dev->base = devm_ioremap_resource(&pdev->dev, res);
123 if (IS_ERR(dev->base))
124 return PTR_ERR(dev->base);
126 dev->clk = devm_clk_get(&pdev->dev, NULL);
127 if (IS_ERR(dev->clk))
128 return PTR_ERR(dev->clk);
130 err = clk_prepare_enable(dev->clk);
134 dev->clk_rate = clk_get_rate(dev->clk);
135 if (!dev->clk_rate) {
140 dev->wdt.parent = &pdev->dev;
141 dev->wdt.info = &tangox_wdt_info;
142 dev->wdt.ops = &tangox_wdt_ops;
143 dev->wdt.timeout = DEFAULT_TIMEOUT;
144 dev->wdt.min_timeout = 1;
145 dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
147 watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
148 watchdog_set_nowayout(&dev->wdt, nowayout);
149 watchdog_set_drvdata(&dev->wdt, dev);
152 * Deactivate counter if disable bit is set to avoid
155 config = readl(dev->base + WD_CONFIG);
156 if (config & WD_CONFIG_DISABLE)
157 writel(0, dev->base + WD_COUNTER);
159 writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
162 * Mark as active and restart with configured timeout if
165 if (readl(dev->base + WD_COUNTER)) {
166 set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
167 tangox_wdt_start(&dev->wdt);
170 watchdog_set_restart_priority(&dev->wdt, 128);
172 err = watchdog_register_device(&dev->wdt);
176 platform_set_drvdata(pdev, dev);
178 dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
183 clk_disable_unprepare(dev->clk);
187 static int tangox_wdt_remove(struct platform_device *pdev)
189 struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
191 tangox_wdt_stop(&dev->wdt);
192 clk_disable_unprepare(dev->clk);
194 watchdog_unregister_device(&dev->wdt);
199 static const struct of_device_id tangox_wdt_dt_ids[] = {
200 { .compatible = "sigma,smp8642-wdt" },
201 { .compatible = "sigma,smp8759-wdt" },
204 MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
206 static struct platform_driver tangox_wdt_driver = {
207 .probe = tangox_wdt_probe,
208 .remove = tangox_wdt_remove,
210 .name = "tangox-wdt",
211 .of_match_table = tangox_wdt_dt_ids,
215 module_platform_driver(tangox_wdt_driver);
217 MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
218 MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
219 MODULE_LICENSE("GPL");