2 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
3 * SMP86xx/SMP87xx Watchdog driver
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
11 #include <linux/bitops.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/notifier.h>
19 #include <linux/platform_device.h>
20 #include <linux/reboot.h>
21 #include <linux/watchdog.h>
23 #define DEFAULT_TIMEOUT 30
25 static bool nowayout = WATCHDOG_NOWAYOUT;
26 module_param(nowayout, bool, 0);
27 MODULE_PARM_DESC(nowayout,
28 "Watchdog cannot be stopped once started (default="
29 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
31 static unsigned int timeout;
32 module_param(timeout, int, 0);
33 MODULE_PARM_DESC(timeout, "Watchdog timeout");
36 * Counter counts down from programmed value. Reset asserts when
37 * the counter reaches 1.
42 #define WD_CONFIG_XTAL_IN BIT(0)
43 #define WD_CONFIG_DISABLE BIT(31)
45 struct tangox_wdt_device {
46 struct watchdog_device wdt;
48 unsigned long clk_rate;
50 struct notifier_block restart;
53 static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
54 unsigned int new_timeout)
56 wdt->timeout = new_timeout;
61 static int tangox_wdt_start(struct watchdog_device *wdt)
63 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
66 ticks = 1 + wdt->timeout * dev->clk_rate;
67 writel(ticks, dev->base + WD_COUNTER);
72 static int tangox_wdt_stop(struct watchdog_device *wdt)
74 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
76 writel(0, dev->base + WD_COUNTER);
81 static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
83 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
86 count = readl(dev->base + WD_COUNTER);
91 return (count - 1) / dev->clk_rate;
94 static const struct watchdog_info tangox_wdt_info = {
95 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
96 .identity = "tangox watchdog",
99 static const struct watchdog_ops tangox_wdt_ops = {
100 .start = tangox_wdt_start,
101 .stop = tangox_wdt_stop,
102 .set_timeout = tangox_wdt_set_timeout,
103 .get_timeleft = tangox_wdt_get_timeleft,
106 static int tangox_wdt_restart(struct notifier_block *nb, unsigned long action,
109 struct tangox_wdt_device *dev =
110 container_of(nb, struct tangox_wdt_device, restart);
112 writel(1, dev->base + WD_COUNTER);
117 static int tangox_wdt_probe(struct platform_device *pdev)
119 struct tangox_wdt_device *dev;
120 struct resource *res;
124 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
128 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
129 dev->base = devm_ioremap_resource(&pdev->dev, res);
130 if (IS_ERR(dev->base))
131 return PTR_ERR(dev->base);
133 dev->clk = devm_clk_get(&pdev->dev, NULL);
134 if (IS_ERR(dev->clk))
135 return PTR_ERR(dev->clk);
137 err = clk_prepare_enable(dev->clk);
141 dev->clk_rate = clk_get_rate(dev->clk);
142 if (!dev->clk_rate) {
147 dev->wdt.parent = &pdev->dev;
148 dev->wdt.info = &tangox_wdt_info;
149 dev->wdt.ops = &tangox_wdt_ops;
150 dev->wdt.timeout = DEFAULT_TIMEOUT;
151 dev->wdt.min_timeout = 1;
152 dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
154 watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
155 watchdog_set_nowayout(&dev->wdt, nowayout);
156 watchdog_set_drvdata(&dev->wdt, dev);
159 * Deactivate counter if disable bit is set to avoid
162 config = readl(dev->base + WD_CONFIG);
163 if (config & WD_CONFIG_DISABLE)
164 writel(0, dev->base + WD_COUNTER);
166 writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
169 * Mark as active and restart with configured timeout if
172 if (readl(dev->base + WD_COUNTER)) {
173 set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
174 tangox_wdt_start(&dev->wdt);
177 err = watchdog_register_device(&dev->wdt);
181 platform_set_drvdata(pdev, dev);
183 dev->restart.notifier_call = tangox_wdt_restart;
184 dev->restart.priority = 128;
185 err = register_restart_handler(&dev->restart);
187 dev_warn(&pdev->dev, "failed to register restart handler\n");
189 dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
194 clk_disable_unprepare(dev->clk);
198 static int tangox_wdt_remove(struct platform_device *pdev)
200 struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
202 tangox_wdt_stop(&dev->wdt);
203 clk_disable_unprepare(dev->clk);
205 unregister_restart_handler(&dev->restart);
206 watchdog_unregister_device(&dev->wdt);
211 static const struct of_device_id tangox_wdt_dt_ids[] = {
212 { .compatible = "sigma,smp8642-wdt" },
213 { .compatible = "sigma,smp8759-wdt" },
216 MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
218 static struct platform_driver tangox_wdt_driver = {
219 .probe = tangox_wdt_probe,
220 .remove = tangox_wdt_remove,
222 .name = "tangox-wdt",
223 .of_match_table = tangox_wdt_dt_ids,
227 module_platform_driver(tangox_wdt_driver);
229 MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
230 MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
231 MODULE_LICENSE("GPL");