blob: d0b53f3c0d171ebaf8d796d371c31f36aa04a837 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Mans Rullgarddca536c2015-11-19 22:09:05 +00002/*
3 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
4 * SMP86xx/SMP87xx Watchdog driver
Mans Rullgarddca536c2015-11-19 22:09:05 +00005 */
6
7#include <linux/bitops.h>
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/io.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
Randy Dunlapac316722018-06-19 22:47:28 -070014#include <linux/mod_devicetable.h>
Mans Rullgarddca536c2015-11-19 22:09:05 +000015#include <linux/platform_device.h>
Mans Rullgarddca536c2015-11-19 22:09:05 +000016#include <linux/watchdog.h>
17
18#define DEFAULT_TIMEOUT 30
19
20static bool nowayout = WATCHDOG_NOWAYOUT;
21module_param(nowayout, bool, 0);
22MODULE_PARM_DESC(nowayout,
23 "Watchdog cannot be stopped once started (default="
24 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
25
26static unsigned int timeout;
27module_param(timeout, int, 0);
28MODULE_PARM_DESC(timeout, "Watchdog timeout");
29
30/*
31 * Counter counts down from programmed value. Reset asserts when
32 * the counter reaches 1.
33 */
34#define WD_COUNTER 0
35
36#define WD_CONFIG 4
37#define WD_CONFIG_XTAL_IN BIT(0)
38#define WD_CONFIG_DISABLE BIT(31)
39
40struct tangox_wdt_device {
41 struct watchdog_device wdt;
42 void __iomem *base;
43 unsigned long clk_rate;
44 struct clk *clk;
Mans Rullgarddca536c2015-11-19 22:09:05 +000045};
46
47static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
48 unsigned int new_timeout)
49{
50 wdt->timeout = new_timeout;
51
52 return 0;
53}
54
55static int tangox_wdt_start(struct watchdog_device *wdt)
56{
57 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
58 u32 ticks;
59
60 ticks = 1 + wdt->timeout * dev->clk_rate;
61 writel(ticks, dev->base + WD_COUNTER);
62
63 return 0;
64}
65
66static int tangox_wdt_stop(struct watchdog_device *wdt)
67{
68 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
69
70 writel(0, dev->base + WD_COUNTER);
71
72 return 0;
73}
74
75static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
76{
77 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
78 u32 count;
79
80 count = readl(dev->base + WD_COUNTER);
81
82 if (!count)
83 return 0;
84
85 return (count - 1) / dev->clk_rate;
86}
87
88static const struct watchdog_info tangox_wdt_info = {
89 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
90 .identity = "tangox watchdog",
91};
92
Guenter Roeck0397c5d2017-01-04 12:28:01 -080093static int tangox_wdt_restart(struct watchdog_device *wdt,
94 unsigned long action, void *data)
95{
96 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
97
98 writel(1, dev->base + WD_COUNTER);
99
100 return 0;
101}
102
Mans Rullgarddca536c2015-11-19 22:09:05 +0000103static const struct watchdog_ops tangox_wdt_ops = {
104 .start = tangox_wdt_start,
105 .stop = tangox_wdt_stop,
106 .set_timeout = tangox_wdt_set_timeout,
107 .get_timeleft = tangox_wdt_get_timeleft,
Guenter Roeck0397c5d2017-01-04 12:28:01 -0800108 .restart = tangox_wdt_restart,
Mans Rullgarddca536c2015-11-19 22:09:05 +0000109};
110
Mans Rullgarddca536c2015-11-19 22:09:05 +0000111static int tangox_wdt_probe(struct platform_device *pdev)
112{
113 struct tangox_wdt_device *dev;
114 struct resource *res;
115 u32 config;
116 int err;
117
118 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
119 if (!dev)
120 return -ENOMEM;
121
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123 dev->base = devm_ioremap_resource(&pdev->dev, res);
124 if (IS_ERR(dev->base))
125 return PTR_ERR(dev->base);
126
127 dev->clk = devm_clk_get(&pdev->dev, NULL);
128 if (IS_ERR(dev->clk))
129 return PTR_ERR(dev->clk);
130
131 err = clk_prepare_enable(dev->clk);
132 if (err)
133 return err;
134
135 dev->clk_rate = clk_get_rate(dev->clk);
Wolfram Sang84b84bc2016-03-03 09:24:12 +0100136 if (!dev->clk_rate) {
137 err = -EINVAL;
138 goto err;
139 }
Mans Rullgarddca536c2015-11-19 22:09:05 +0000140
141 dev->wdt.parent = &pdev->dev;
142 dev->wdt.info = &tangox_wdt_info;
143 dev->wdt.ops = &tangox_wdt_ops;
144 dev->wdt.timeout = DEFAULT_TIMEOUT;
145 dev->wdt.min_timeout = 1;
Guenter Roeckc7ef68c32016-07-17 13:47:47 -0700146 dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
Mans Rullgarddca536c2015-11-19 22:09:05 +0000147
148 watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
149 watchdog_set_nowayout(&dev->wdt, nowayout);
150 watchdog_set_drvdata(&dev->wdt, dev);
151
152 /*
153 * Deactivate counter if disable bit is set to avoid
154 * accidental reset.
155 */
156 config = readl(dev->base + WD_CONFIG);
157 if (config & WD_CONFIG_DISABLE)
158 writel(0, dev->base + WD_COUNTER);
159
160 writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
161
162 /*
163 * Mark as active and restart with configured timeout if
164 * already running.
165 */
166 if (readl(dev->base + WD_COUNTER)) {
Guenter Roecka3e376d2016-05-27 15:28:00 -0700167 set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
Mans Rullgarddca536c2015-11-19 22:09:05 +0000168 tangox_wdt_start(&dev->wdt);
169 }
170
Guenter Roeck0397c5d2017-01-04 12:28:01 -0800171 watchdog_set_restart_priority(&dev->wdt, 128);
172
Mans Rullgarddca536c2015-11-19 22:09:05 +0000173 err = watchdog_register_device(&dev->wdt);
Wolfram Sang84b84bc2016-03-03 09:24:12 +0100174 if (err)
175 goto err;
Mans Rullgarddca536c2015-11-19 22:09:05 +0000176
177 platform_set_drvdata(pdev, dev);
178
Guenter Roeck3d29f802015-12-24 14:22:01 -0800179 dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
Mans Rullgarddca536c2015-11-19 22:09:05 +0000180
181 return 0;
Wolfram Sang84b84bc2016-03-03 09:24:12 +0100182
183 err:
184 clk_disable_unprepare(dev->clk);
185 return err;
Mans Rullgarddca536c2015-11-19 22:09:05 +0000186}
187
188static int tangox_wdt_remove(struct platform_device *pdev)
189{
190 struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
191
192 tangox_wdt_stop(&dev->wdt);
193 clk_disable_unprepare(dev->clk);
194
Mans Rullgarddca536c2015-11-19 22:09:05 +0000195 watchdog_unregister_device(&dev->wdt);
196
197 return 0;
198}
199
200static const struct of_device_id tangox_wdt_dt_ids[] = {
201 { .compatible = "sigma,smp8642-wdt" },
202 { .compatible = "sigma,smp8759-wdt" },
203 { }
204};
205MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
206
207static struct platform_driver tangox_wdt_driver = {
208 .probe = tangox_wdt_probe,
209 .remove = tangox_wdt_remove,
210 .driver = {
211 .name = "tangox-wdt",
212 .of_match_table = tangox_wdt_dt_ids,
213 },
214};
215
216module_platform_driver(tangox_wdt_driver);
217
218MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
219MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
220MODULE_LICENSE("GPL");