blob: 77afb0acc50045a88a55282e8009f6b2ddd827d2 [file] [log] [blame]
Thierry Reding38461c52009-09-23 13:49:52 +02001/*
2 * Copyright (C) 2008-2009 Avionic Design GmbH
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/fs.h>
10#include <linux/io.h>
11#include <linux/miscdevice.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/types.h>
15#include <linux/uaccess.h>
16#include <linux/watchdog.h>
17
18#define WATCHDOG_NAME "adx-wdt"
19
20/* register offsets */
21#define ADX_WDT_CONTROL 0x00
22#define ADX_WDT_CONTROL_ENABLE (1 << 0)
23#define ADX_WDT_CONTROL_nRESET (1 << 1)
24#define ADX_WDT_TIMEOUT 0x08
25
26static struct platform_device *adx_wdt_dev;
27static unsigned long driver_open;
28
29#define WDT_STATE_STOP 0
30#define WDT_STATE_START 1
31
32struct adx_wdt {
33 void __iomem *base;
34 unsigned long timeout;
35 unsigned int state;
36 unsigned int wake;
37 spinlock_t lock;
38};
39
40static struct watchdog_info adx_wdt_info = {
41 .identity = "Avionic Design Xanthos Watchdog",
42 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
43};
44
45static void adx_wdt_start_locked(struct adx_wdt *wdt)
46{
47 u32 ctrl;
48
49 ctrl = readl(wdt->base + ADX_WDT_CONTROL);
50 ctrl |= ADX_WDT_CONTROL_ENABLE;
51 writel(ctrl, wdt->base + ADX_WDT_CONTROL);
52 wdt->state = WDT_STATE_START;
53}
54
55static void adx_wdt_start(struct adx_wdt *wdt)
56{
57 unsigned long flags;
58
59 spin_lock_irqsave(&wdt->lock, flags);
60 adx_wdt_start_locked(wdt);
61 spin_unlock_irqrestore(&wdt->lock, flags);
62}
63
64static void adx_wdt_stop_locked(struct adx_wdt *wdt)
65{
66 u32 ctrl;
67
68 ctrl = readl(wdt->base + ADX_WDT_CONTROL);
69 ctrl &= ~ADX_WDT_CONTROL_ENABLE;
70 writel(ctrl, wdt->base + ADX_WDT_CONTROL);
71 wdt->state = WDT_STATE_STOP;
72}
73
74static void adx_wdt_stop(struct adx_wdt *wdt)
75{
76 unsigned long flags;
77
78 spin_lock_irqsave(&wdt->lock, flags);
79 adx_wdt_stop_locked(wdt);
80 spin_unlock_irqrestore(&wdt->lock, flags);
81}
82
83static void adx_wdt_set_timeout(struct adx_wdt *wdt, unsigned long seconds)
84{
85 unsigned long timeout = seconds * 1000;
86 unsigned long flags;
87 unsigned int state;
88
89 spin_lock_irqsave(&wdt->lock, flags);
90 state = wdt->state;
91 adx_wdt_stop_locked(wdt);
92 writel(timeout, wdt->base + ADX_WDT_TIMEOUT);
93
94 if (state == WDT_STATE_START)
95 adx_wdt_start_locked(wdt);
96
97 wdt->timeout = timeout;
98 spin_unlock_irqrestore(&wdt->lock, flags);
99}
100
101static void adx_wdt_get_timeout(struct adx_wdt *wdt, unsigned long *seconds)
102{
103 *seconds = wdt->timeout / 1000;
104}
105
106static void adx_wdt_keepalive(struct adx_wdt *wdt)
107{
108 unsigned long flags;
109
110 spin_lock_irqsave(&wdt->lock, flags);
111 writel(wdt->timeout, wdt->base + ADX_WDT_TIMEOUT);
112 spin_unlock_irqrestore(&wdt->lock, flags);
113}
114
115static int adx_wdt_open(struct inode *inode, struct file *file)
116{
117 struct adx_wdt *wdt = platform_get_drvdata(adx_wdt_dev);
118
119 if (test_and_set_bit(0, &driver_open))
120 return -EBUSY;
121
122 file->private_data = wdt;
123 adx_wdt_set_timeout(wdt, 30);
124 adx_wdt_start(wdt);
125
126 return nonseekable_open(inode, file);
127}
128
129static int adx_wdt_release(struct inode *inode, struct file *file)
130{
131 struct adx_wdt *wdt = file->private_data;
132
133 adx_wdt_stop(wdt);
134 clear_bit(0, &driver_open);
135
136 return 0;
137}
138
139static long adx_wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
140{
141 struct adx_wdt *wdt = file->private_data;
142 void __user *argp = (void __user *)arg;
143 unsigned long __user *p = argp;
144 unsigned long seconds = 0;
145 unsigned int options;
146 long ret = -EINVAL;
147
148 switch (cmd) {
149 case WDIOC_GETSUPPORT:
150 if (copy_to_user(argp, &adx_wdt_info, sizeof(adx_wdt_info)))
151 return -EFAULT;
152 else
153 return 0;
154
155 case WDIOC_GETSTATUS:
156 case WDIOC_GETBOOTSTATUS:
157 return put_user(0, p);
158
159 case WDIOC_KEEPALIVE:
160 adx_wdt_keepalive(wdt);
161 return 0;
162
163 case WDIOC_SETTIMEOUT:
164 if (get_user(seconds, p))
165 return -EFAULT;
166
167 adx_wdt_set_timeout(wdt, seconds);
168
169 /* fallthrough */
170 case WDIOC_GETTIMEOUT:
171 adx_wdt_get_timeout(wdt, &seconds);
172 return put_user(seconds, p);
173
174 case WDIOC_SETOPTIONS:
175 if (copy_from_user(&options, argp, sizeof(options)))
176 return -EFAULT;
177
178 if (options & WDIOS_DISABLECARD) {
179 adx_wdt_stop(wdt);
180 ret = 0;
181 }
182
183 if (options & WDIOS_ENABLECARD) {
184 adx_wdt_start(wdt);
185 ret = 0;
186 }
187
188 return ret;
189
190 default:
191 break;
192 }
193
194 return -ENOTTY;
195}
196
197static ssize_t adx_wdt_write(struct file *file, const char __user *data,
198 size_t len, loff_t *ppos)
199{
200 struct adx_wdt *wdt = file->private_data;
201
202 if (len)
203 adx_wdt_keepalive(wdt);
204
205 return len;
206}
207
208static const struct file_operations adx_wdt_fops = {
209 .owner = THIS_MODULE,
210 .llseek = no_llseek,
211 .open = adx_wdt_open,
212 .release = adx_wdt_release,
213 .unlocked_ioctl = adx_wdt_ioctl,
214 .write = adx_wdt_write,
215};
216
217static struct miscdevice adx_wdt_miscdev = {
218 .minor = WATCHDOG_MINOR,
219 .name = "watchdog",
220 .fops = &adx_wdt_fops,
221};
222
223static int __devinit adx_wdt_probe(struct platform_device *pdev)
224{
225 struct resource *res;
226 struct adx_wdt *wdt;
227 int ret = 0;
228 u32 ctrl;
229
230 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
231 if (!wdt) {
232 dev_err(&pdev->dev, "cannot allocate WDT structure\n");
233 return -ENOMEM;
234 }
235
236 spin_lock_init(&wdt->lock);
237
238 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
239 if (!res) {
240 dev_err(&pdev->dev, "cannot obtain I/O memory region\n");
241 return -ENXIO;
242 }
243
244 res = devm_request_mem_region(&pdev->dev, res->start,
245 res->end - res->start + 1, res->name);
246 if (!res) {
247 dev_err(&pdev->dev, "cannot request I/O memory region\n");
248 return -ENXIO;
249 }
250
251 wdt->base = devm_ioremap_nocache(&pdev->dev, res->start,
252 res->end - res->start + 1);
253 if (!wdt->base) {
254 dev_err(&pdev->dev, "cannot remap I/O memory region\n");
255 return -ENXIO;
256 }
257
258 /* disable watchdog and reboot on timeout */
259 ctrl = readl(wdt->base + ADX_WDT_CONTROL);
260 ctrl &= ~ADX_WDT_CONTROL_ENABLE;
261 ctrl &= ~ADX_WDT_CONTROL_nRESET;
262 writel(ctrl, wdt->base + ADX_WDT_CONTROL);
263
264 platform_set_drvdata(pdev, wdt);
265 adx_wdt_dev = pdev;
266
267 ret = misc_register(&adx_wdt_miscdev);
268 if (ret) {
269 dev_err(&pdev->dev, "cannot register miscdev on minor %d "
270 "(err=%d)\n", WATCHDOG_MINOR, ret);
271 return ret;
272 }
273
274 return 0;
275}
276
277static int __devexit adx_wdt_remove(struct platform_device *pdev)
278{
279 struct adx_wdt *wdt = platform_get_drvdata(pdev);
280
281 misc_deregister(&adx_wdt_miscdev);
282 adx_wdt_stop(wdt);
283 platform_set_drvdata(pdev, NULL);
284
285 return 0;
286}
287
288static void adx_wdt_shutdown(struct platform_device *pdev)
289{
290 struct adx_wdt *wdt = platform_get_drvdata(pdev);
291 adx_wdt_stop(wdt);
292}
293
294#ifdef CONFIG_PM
295static int adx_wdt_suspend(struct device *dev)
296{
297 struct platform_device *pdev = to_platform_device(dev);
298 struct adx_wdt *wdt = platform_get_drvdata(pdev);
299
300 wdt->wake = (wdt->state == WDT_STATE_START) ? 1 : 0;
301 adx_wdt_stop(wdt);
302
303 return 0;
304}
305
306static int adx_wdt_resume(struct device *dev)
307{
308 struct platform_device *pdev = to_platform_device(dev);
309 struct adx_wdt *wdt = platform_get_drvdata(pdev);
310
311 if (wdt->wake)
312 adx_wdt_start(wdt);
313
314 return 0;
315}
316
317static struct dev_pm_ops adx_wdt_pm_ops = {
318 .suspend = adx_wdt_suspend,
319 .resume = adx_wdt_resume,
320};
321
322# define ADX_WDT_PM_OPS (&adx_wdt_pm_ops)
323#else
324# define ADX_WDT_PM_OPS NULL
325#endif
326
327static struct platform_driver adx_wdt_driver = {
328 .probe = adx_wdt_probe,
329 .remove = __devexit_p(adx_wdt_remove),
330 .shutdown = adx_wdt_shutdown,
331 .driver = {
332 .name = WATCHDOG_NAME,
333 .owner = THIS_MODULE,
334 .pm = ADX_WDT_PM_OPS,
335 },
336};
337
338static int __init adx_wdt_init(void)
339{
340 return platform_driver_register(&adx_wdt_driver);
341}
342
343static void __exit adx_wdt_exit(void)
344{
345 platform_driver_unregister(&adx_wdt_driver);
346}
347
348module_init(adx_wdt_init);
349module_exit(adx_wdt_exit);
350
351MODULE_DESCRIPTION("Avionic Design Xanthos Watchdog Driver");
352MODULE_LICENSE("GPL v2");
353MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
354MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);