1
2
3
4
5
6
7
8
9
10
11#include <linux/bitops.h>
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/platform_device.h>
19#include <linux/watchdog.h>
20
21#define DEFAULT_TIMEOUT 30
22
23static bool nowayout = WATCHDOG_NOWAYOUT;
24module_param(nowayout, bool, 0);
25MODULE_PARM_DESC(nowayout,
26 "Watchdog cannot be stopped once started (default="
27 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
28
29static unsigned int timeout;
30module_param(timeout, int, 0);
31MODULE_PARM_DESC(timeout, "Watchdog timeout");
32
33
34
35
36
37#define WD_COUNTER 0
38
39#define WD_CONFIG 4
40#define WD_CONFIG_XTAL_IN BIT(0)
41#define WD_CONFIG_DISABLE BIT(31)
42
43struct tangox_wdt_device {
44 struct watchdog_device wdt;
45 void __iomem *base;
46 unsigned long clk_rate;
47 struct clk *clk;
48};
49
50static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
51 unsigned int new_timeout)
52{
53 wdt->timeout = new_timeout;
54
55 return 0;
56}
57
58static int tangox_wdt_start(struct watchdog_device *wdt)
59{
60 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
61 u32 ticks;
62
63 ticks = 1 + wdt->timeout * dev->clk_rate;
64 writel(ticks, dev->base + WD_COUNTER);
65
66 return 0;
67}
68
69static int tangox_wdt_stop(struct watchdog_device *wdt)
70{
71 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
72
73 writel(0, dev->base + WD_COUNTER);
74
75 return 0;
76}
77
78static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
79{
80 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
81 u32 count;
82
83 count = readl(dev->base + WD_COUNTER);
84
85 if (!count)
86 return 0;
87
88 return (count - 1) / dev->clk_rate;
89}
90
91static const struct watchdog_info tangox_wdt_info = {
92 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
93 .identity = "tangox watchdog",
94};
95
96static int tangox_wdt_restart(struct watchdog_device *wdt,
97 unsigned long action, void *data)
98{
99 struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
100
101 writel(1, dev->base + WD_COUNTER);
102
103 return 0;
104}
105
106static const struct watchdog_ops tangox_wdt_ops = {
107 .start = tangox_wdt_start,
108 .stop = tangox_wdt_stop,
109 .set_timeout = tangox_wdt_set_timeout,
110 .get_timeleft = tangox_wdt_get_timeleft,
111 .restart = tangox_wdt_restart,
112};
113
114static int tangox_wdt_probe(struct platform_device *pdev)
115{
116 struct tangox_wdt_device *dev;
117 struct resource *res;
118 u32 config;
119 int err;
120
121 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
122 if (!dev)
123 return -ENOMEM;
124
125 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
126 dev->base = devm_ioremap_resource(&pdev->dev, res);
127 if (IS_ERR(dev->base))
128 return PTR_ERR(dev->base);
129
130 dev->clk = devm_clk_get(&pdev->dev, NULL);
131 if (IS_ERR(dev->clk))
132 return PTR_ERR(dev->clk);
133
134 err = clk_prepare_enable(dev->clk);
135 if (err)
136 return err;
137
138 dev->clk_rate = clk_get_rate(dev->clk);
139 if (!dev->clk_rate) {
140 err = -EINVAL;
141 goto err;
142 }
143
144 dev->wdt.parent = &pdev->dev;
145 dev->wdt.info = &tangox_wdt_info;
146 dev->wdt.ops = &tangox_wdt_ops;
147 dev->wdt.timeout = DEFAULT_TIMEOUT;
148 dev->wdt.min_timeout = 1;
149 dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
150
151 watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
152 watchdog_set_nowayout(&dev->wdt, nowayout);
153 watchdog_set_drvdata(&dev->wdt, dev);
154
155
156
157
158
159 config = readl(dev->base + WD_CONFIG);
160 if (config & WD_CONFIG_DISABLE)
161 writel(0, dev->base + WD_COUNTER);
162
163 writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
164
165
166
167
168
169 if (readl(dev->base + WD_COUNTER)) {
170 set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
171 tangox_wdt_start(&dev->wdt);
172 }
173
174 watchdog_set_restart_priority(&dev->wdt, 128);
175
176 err = watchdog_register_device(&dev->wdt);
177 if (err)
178 goto err;
179
180 platform_set_drvdata(pdev, dev);
181
182 dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
183
184 return 0;
185
186 err:
187 clk_disable_unprepare(dev->clk);
188 return err;
189}
190
191static int tangox_wdt_remove(struct platform_device *pdev)
192{
193 struct tangox_wdt_device *dev = platform_get_drvdata(pdev);
194
195 tangox_wdt_stop(&dev->wdt);
196 clk_disable_unprepare(dev->clk);
197
198 watchdog_unregister_device(&dev->wdt);
199
200 return 0;
201}
202
203static const struct of_device_id tangox_wdt_dt_ids[] = {
204 { .compatible = "sigma,smp8642-wdt" },
205 { .compatible = "sigma,smp8759-wdt" },
206 { }
207};
208MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
209
210static struct platform_driver tangox_wdt_driver = {
211 .probe = tangox_wdt_probe,
212 .remove = tangox_wdt_remove,
213 .driver = {
214 .name = "tangox-wdt",
215 .of_match_table = tangox_wdt_dt_ids,
216 },
217};
218
219module_platform_driver(tangox_wdt_driver);
220
221MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
222MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
223MODULE_LICENSE("GPL");
224