[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / watchdog / adx_wdt.c
blob77afb0acc50045a88a55282e8009f6b2ddd827d2
1 /*
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.
7 */
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>
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
32 struct adx_wdt {
33 void __iomem *base;
34 unsigned long timeout;
35 unsigned int state;
36 unsigned int wake;
37 spinlock_t lock;
40 static 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)
47 u32 ctrl;
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)
57 unsigned long flags;
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)
66 u32 ctrl;
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)
76 unsigned long flags;
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;
86 unsigned long flags;
87 unsigned int state;
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);
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)
108 unsigned long flags;
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))
120 return -EBUSY;
122 file->private_data = wdt;
123 adx_wdt_set_timeout(wdt, 30);
124 adx_wdt_start(wdt);
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;
133 adx_wdt_stop(wdt);
134 clear_bit(0, &driver_open);
136 return 0;
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;
146 long ret = -EINVAL;
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;
155 case WDIOC_GETSTATUS:
156 case WDIOC_GETBOOTSTATUS:
157 return put_user(0, p);
159 case WDIOC_KEEPALIVE:
160 adx_wdt_keepalive(wdt);
161 return 0;
163 case WDIOC_SETTIMEOUT:
164 if (get_user(seconds, p))
165 return -EFAULT;
167 adx_wdt_set_timeout(wdt, seconds);
169 /* fallthrough */
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)))
176 return -EFAULT;
178 if (options & WDIOS_DISABLECARD) {
179 adx_wdt_stop(wdt);
180 ret = 0;
183 if (options & WDIOS_ENABLECARD) {
184 adx_wdt_start(wdt);
185 ret = 0;
188 return ret;
190 default:
191 break;
194 return -ENOTTY;
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;
202 if (len)
203 adx_wdt_keepalive(wdt);
205 return len;
208 static 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,
217 static struct miscdevice adx_wdt_miscdev = {
218 .minor = WATCHDOG_MINOR,
219 .name = "watchdog",
220 .fops = &adx_wdt_fops,
223 static int __devinit adx_wdt_probe(struct platform_device *pdev)
225 struct resource *res;
226 struct adx_wdt *wdt;
227 int ret = 0;
228 u32 ctrl;
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;
236 spin_lock_init(&wdt->lock);
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;
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;
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;
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);
265 adx_wdt_dev = pdev;
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;
274 return 0;
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);
282 adx_wdt_stop(wdt);
283 platform_set_drvdata(pdev, NULL);
285 return 0;
288 static void adx_wdt_shutdown(struct platform_device *pdev)
290 struct adx_wdt *wdt = platform_get_drvdata(pdev);
291 adx_wdt_stop(wdt);
294 #ifdef CONFIG_PM
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;
301 adx_wdt_stop(wdt);
303 return 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);
311 if (wdt->wake)
312 adx_wdt_start(wdt);
314 return 0;
317 static 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)
323 #else
324 # define ADX_WDT_PM_OPS NULL
325 #endif
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,
331 .driver = {
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);