2 * Copyright (C) 2008-2009 Avionic Design GmbH
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.
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>
18 #define WATCHDOG_NAME "adx-wdt"
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
26 static struct platform_device *adx_wdt_dev;
27 static unsigned long driver_open;
29 #define WDT_STATE_STOP 0
30 #define WDT_STATE_START 1
34 unsigned long timeout;
40 static const struct watchdog_info adx_wdt_info = {
41 .identity = "Avionic Design Xanthos Watchdog",
42 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
45 static void adx_wdt_start_locked(struct adx_wdt *wdt)
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;
55 static void adx_wdt_start(struct adx_wdt *wdt)
59 spin_lock_irqsave(&wdt->lock, flags);
60 adx_wdt_start_locked(wdt);
61 spin_unlock_irqrestore(&wdt->lock, flags);
64 static void adx_wdt_stop_locked(struct adx_wdt *wdt)
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;
74 static void adx_wdt_stop(struct adx_wdt *wdt)
78 spin_lock_irqsave(&wdt->lock, flags);
79 adx_wdt_stop_locked(wdt);
80 spin_unlock_irqrestore(&wdt->lock, flags);
83 static void adx_wdt_set_timeout(struct adx_wdt *wdt, unsigned long seconds)
85 unsigned long timeout = seconds * 1000;
89 spin_lock_irqsave(&wdt->lock, flags);
91 adx_wdt_stop_locked(wdt);
92 writel(timeout, wdt->base + ADX_WDT_TIMEOUT);
94 if (state == WDT_STATE_START)
95 adx_wdt_start_locked(wdt);
97 wdt->timeout = timeout;
98 spin_unlock_irqrestore(&wdt->lock, flags);
101 static void adx_wdt_get_timeout(struct adx_wdt *wdt, unsigned long *seconds)
103 *seconds = wdt->timeout / 1000;
106 static void adx_wdt_keepalive(struct adx_wdt *wdt)
110 spin_lock_irqsave(&wdt->lock, flags);
111 writel(wdt->timeout, wdt->base + ADX_WDT_TIMEOUT);
112 spin_unlock_irqrestore(&wdt->lock, flags);
115 static int adx_wdt_open(struct inode *inode, struct file *file)
117 struct adx_wdt *wdt = platform_get_drvdata(adx_wdt_dev);
119 if (test_and_set_bit(0, &driver_open))
122 file->private_data = wdt;
123 adx_wdt_set_timeout(wdt, 30);
126 return nonseekable_open(inode, file);
129 static int adx_wdt_release(struct inode *inode, struct file *file)
131 struct adx_wdt *wdt = file->private_data;
134 clear_bit(0, &driver_open);
139 static long adx_wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
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;
149 case WDIOC_GETSUPPORT:
150 if (copy_to_user(argp, &adx_wdt_info, sizeof(adx_wdt_info)))
155 case WDIOC_GETSTATUS:
156 case WDIOC_GETBOOTSTATUS:
157 return put_user(0, p);
159 case WDIOC_KEEPALIVE:
160 adx_wdt_keepalive(wdt);
163 case WDIOC_SETTIMEOUT:
164 if (get_user(seconds, p))
167 adx_wdt_set_timeout(wdt, seconds);
170 case WDIOC_GETTIMEOUT:
171 adx_wdt_get_timeout(wdt, &seconds);
172 return put_user(seconds, p);
174 case WDIOC_SETOPTIONS:
175 if (copy_from_user(&options, argp, sizeof(options)))
178 if (options & WDIOS_DISABLECARD) {
183 if (options & WDIOS_ENABLECARD) {
197 static ssize_t adx_wdt_write(struct file *file, const char __user *data,
198 size_t len, loff_t *ppos)
200 struct adx_wdt *wdt = file->private_data;
203 adx_wdt_keepalive(wdt);
208 static const struct file_operations adx_wdt_fops = {
209 .owner = THIS_MODULE,
211 .open = adx_wdt_open,
212 .release = adx_wdt_release,
213 .unlocked_ioctl = adx_wdt_ioctl,
214 .write = adx_wdt_write,
217 static struct miscdevice adx_wdt_miscdev = {
218 .minor = WATCHDOG_MINOR,
220 .fops = &adx_wdt_fops,
223 static int __devinit adx_wdt_probe(struct platform_device *pdev)
225 struct resource *res;
230 wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
232 dev_err(&pdev->dev, "cannot allocate WDT structure\n");
236 spin_lock_init(&wdt->lock);
238 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
240 dev_err(&pdev->dev, "cannot obtain I/O memory region\n");
244 res = devm_request_mem_region(&pdev->dev, res->start,
245 resource_size(res), res->name);
247 dev_err(&pdev->dev, "cannot request I/O memory region\n");
251 wdt->base = devm_ioremap_nocache(&pdev->dev, res->start,
254 dev_err(&pdev->dev, "cannot remap I/O memory region\n");
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);
264 platform_set_drvdata(pdev, wdt);
267 ret = misc_register(&adx_wdt_miscdev);
269 dev_err(&pdev->dev, "cannot register miscdev on minor %d "
270 "(err=%d)\n", WATCHDOG_MINOR, ret);
277 static int __devexit adx_wdt_remove(struct platform_device *pdev)
279 struct adx_wdt *wdt = platform_get_drvdata(pdev);
281 misc_deregister(&adx_wdt_miscdev);
283 platform_set_drvdata(pdev, NULL);
288 static void adx_wdt_shutdown(struct platform_device *pdev)
290 struct adx_wdt *wdt = platform_get_drvdata(pdev);
295 static int adx_wdt_suspend(struct device *dev)
297 struct platform_device *pdev = to_platform_device(dev);
298 struct adx_wdt *wdt = platform_get_drvdata(pdev);
300 wdt->wake = (wdt->state == WDT_STATE_START) ? 1 : 0;
306 static int adx_wdt_resume(struct device *dev)
308 struct platform_device *pdev = to_platform_device(dev);
309 struct adx_wdt *wdt = platform_get_drvdata(pdev);
317 static const struct dev_pm_ops adx_wdt_pm_ops = {
318 .suspend = adx_wdt_suspend,
319 .resume = adx_wdt_resume,
322 # define ADX_WDT_PM_OPS (&adx_wdt_pm_ops)
324 # define ADX_WDT_PM_OPS NULL
327 static struct platform_driver adx_wdt_driver = {
328 .probe = adx_wdt_probe,
329 .remove = __devexit_p(adx_wdt_remove),
330 .shutdown = adx_wdt_shutdown,
332 .name = WATCHDOG_NAME,
333 .owner = THIS_MODULE,
334 .pm = ADX_WDT_PM_OPS,
338 static int __init adx_wdt_init(void)
340 return platform_driver_register(&adx_wdt_driver);
343 static void __exit adx_wdt_exit(void)
345 platform_driver_unregister(&adx_wdt_driver);
348 module_init(adx_wdt_init);
349 module_exit(adx_wdt_exit);
351 MODULE_DESCRIPTION("Avionic Design Xanthos Watchdog Driver");
352 MODULE_LICENSE("GPL v2");
353 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
354 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);