[WATCHDOG] at32ap700x_wdt.c - Add nowayout + MAGICCLOSE features
[linux-2.6/btrfs-unstable.git] / drivers / char / watchdog / at32ap700x_wdt.c
blobfcd55c600b87731c93e1f4f3becdcbfcda6897a7
1 /*
2 * Watchdog driver for Atmel AT32AP700X devices
4 * Copyright (C) 2005-2006 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/miscdevice.h>
16 #include <linux/fs.h>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
19 #include <linux/uaccess.h>
20 #include <linux/io.h>
22 #define TIMEOUT_MIN 1
23 #define TIMEOUT_MAX 2
24 #define TIMEOUT_DEFAULT TIMEOUT_MAX
26 /* module parameters */
27 static int timeout = TIMEOUT_DEFAULT;
28 module_param(timeout, int, 0);
29 MODULE_PARM_DESC(timeout,
30 "Timeout value. Limited to be 1 or 2 seconds. (default="
31 __MODULE_STRING(TIMEOUT_DEFAULT) ")");
33 static int nowayout = WATCHDOG_NOWAYOUT;
34 module_param(nowayout, int, 0);
35 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
36 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
38 /* Watchdog registers and write/read macro */
39 #define WDT_CTRL 0x00
40 #define WDT_CTRL_EN 0
41 #define WDT_CTRL_PSEL 8
42 #define WDT_CTRL_KEY 24
44 #define WDT_CLR 0x04
46 #define WDT_BIT(name) (1 << WDT_##name)
47 #define WDT_BF(name, value) ((value) << WDT_##name)
49 #define wdt_readl(dev, reg) \
50 __raw_readl((dev)->regs + WDT_##reg)
51 #define wdt_writel(dev, reg, value) \
52 __raw_writel((value), (dev)->regs + WDT_##reg)
54 struct wdt_at32ap700x {
55 void __iomem *regs;
56 int timeout;
57 int users;
58 struct miscdevice miscdev;
61 static struct wdt_at32ap700x *wdt;
62 static char expect_release;
65 * Disable the watchdog.
67 static inline void at32_wdt_stop(void)
69 unsigned long psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
70 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
71 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
75 * Enable and reset the watchdog.
77 static inline void at32_wdt_start(void)
79 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
80 unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
82 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
83 | WDT_BF(CTRL_PSEL, psel)
84 | WDT_BF(CTRL_KEY, 0x55));
85 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
86 | WDT_BF(CTRL_PSEL, psel)
87 | WDT_BF(CTRL_KEY, 0xaa));
91 * Pat the watchdog timer.
93 static inline void at32_wdt_pat(void)
95 wdt_writel(wdt, CLR, 0x42);
99 * Watchdog device is opened, and watchdog starts running.
101 static int at32_wdt_open(struct inode *inode, struct file *file)
103 if (test_and_set_bit(1, &wdt->users))
104 return -EBUSY;
106 at32_wdt_start();
107 return nonseekable_open(inode, file);
111 * Close the watchdog device.
113 static int at32_wdt_close(struct inode *inode, struct file *file)
115 if (expect_release == 42) {
116 at32_wdt_stop();
117 } else {
118 dev_dbg(wdt->miscdev.parent,
119 "Unexpected close, not stopping watchdog!\n");
120 at32_wdt_pat();
122 clear_bit(1, &wdt->users);
123 expect_release = 0;
124 return 0;
128 * Change the watchdog time interval.
130 static int at32_wdt_settimeout(int time)
133 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
134 * 2 ^ 16 allowing up to 2 seconds timeout.
136 if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
137 return -EINVAL;
140 * Set new watchdog time. It will be used when at32_wdt_start() is
141 * called.
143 wdt->timeout = time;
144 return 0;
147 static struct watchdog_info at32_wdt_info = {
148 .identity = "at32ap700x watchdog",
149 .options = WDIOF_SETTIMEOUT |
150 WDIOF_KEEPALIVEPING |
151 WDIOF_MAGICCLOSE,
155 * Handle commands from user-space.
157 static int at32_wdt_ioctl(struct inode *inode, struct file *file,
158 unsigned int cmd, unsigned long arg)
160 int ret = -ENOTTY;
161 int time;
162 void __user *argp = (void __user *)arg;
163 int __user *p = argp;
165 switch (cmd) {
166 case WDIOC_KEEPALIVE:
167 at32_wdt_pat();
168 ret = 0;
169 break;
170 case WDIOC_GETSUPPORT:
171 ret = copy_to_user(argp, &at32_wdt_info,
172 sizeof(at32_wdt_info)) ? -EFAULT : 0;
173 break;
174 case WDIOC_SETTIMEOUT:
175 ret = get_user(time, p);
176 if (ret)
177 break;
178 ret = at32_wdt_settimeout(time);
179 if (ret)
180 break;
181 /* Enable new time value */
182 at32_wdt_start();
183 /* fall through */
184 case WDIOC_GETTIMEOUT:
185 ret = put_user(wdt->timeout, p);
186 break;
187 case WDIOC_GETSTATUS: /* fall through */
188 case WDIOC_GETBOOTSTATUS:
189 ret = put_user(0, p);
190 break;
191 case WDIOC_SETOPTIONS:
192 ret = get_user(time, p);
193 if (ret)
194 break;
195 if (time & WDIOS_DISABLECARD)
196 at32_wdt_stop();
197 if (time & WDIOS_ENABLECARD)
198 at32_wdt_start();
199 ret = 0;
200 break;
203 return ret;
206 static ssize_t at32_wdt_write(struct file *file, const char __user *data,
207 size_t len, loff_t *ppos)
209 /* See if we got the magic character 'V' and reload the timer */
210 if (len) {
211 if (!nowayout) {
212 size_t i;
215 * note: just in case someone wrote the magic
216 * character five months ago...
218 expect_release = 0;
221 * scan to see whether or not we got the magic
222 * character
224 for (i = 0; i != len; i++) {
225 char c;
226 if (get_user(c, data+i))
227 return -EFAULT;
228 if (c == 'V')
229 expect_release = 42;
232 /* someone wrote to us, we should pat the watchdog */
233 at32_wdt_pat();
235 return len;
238 static const struct file_operations at32_wdt_fops = {
239 .owner = THIS_MODULE,
240 .llseek = no_llseek,
241 .ioctl = at32_wdt_ioctl,
242 .open = at32_wdt_open,
243 .release = at32_wdt_close,
244 .write = at32_wdt_write,
247 static int __init at32_wdt_probe(struct platform_device *pdev)
249 struct resource *regs;
250 int ret;
252 if (wdt) {
253 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
254 return -EBUSY;
257 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258 if (!regs) {
259 dev_dbg(&pdev->dev, "missing mmio resource\n");
260 return -ENXIO;
263 wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL);
264 if (!wdt) {
265 dev_dbg(&pdev->dev, "no memory for wdt structure\n");
266 return -ENOMEM;
269 wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
270 if (!wdt->regs) {
271 ret = -ENOMEM;
272 dev_dbg(&pdev->dev, "could not map I/O memory\n");
273 goto err_free;
275 wdt->users = 0;
276 wdt->miscdev.minor = WATCHDOG_MINOR;
277 wdt->miscdev.name = "watchdog";
278 wdt->miscdev.fops = &at32_wdt_fops;
280 if (at32_wdt_settimeout(timeout)) {
281 at32_wdt_settimeout(TIMEOUT_DEFAULT);
282 dev_dbg(&pdev->dev,
283 "default timeout invalid, set to %d sec.\n",
284 TIMEOUT_DEFAULT);
287 ret = misc_register(&wdt->miscdev);
288 if (ret) {
289 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
290 goto err_iounmap;
293 platform_set_drvdata(pdev, wdt);
294 wdt->miscdev.parent = &pdev->dev;
295 dev_info(&pdev->dev,
296 "AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
297 wdt->regs, wdt->timeout, nowayout);
299 return 0;
301 err_iounmap:
302 iounmap(wdt->regs);
303 err_free:
304 kfree(wdt);
305 wdt = NULL;
306 return ret;
309 static int __exit at32_wdt_remove(struct platform_device *pdev)
311 if (wdt && platform_get_drvdata(pdev) == wdt) {
312 /* Stop the timer before we leave */
313 if (!nowayout)
314 at32_wdt_stop();
316 misc_deregister(&wdt->miscdev);
317 iounmap(wdt->regs);
318 kfree(wdt);
319 wdt = NULL;
320 platform_set_drvdata(pdev, NULL);
323 return 0;
326 static void at32_wdt_shutdown(struct platform_device *pdev)
328 at32_wdt_stop();
331 #ifdef CONFIG_PM
332 static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
334 at32_wdt_stop();
335 return 0;
338 static int at32_wdt_resume(struct platform_device *pdev)
340 if (wdt->users)
341 at32_wdt_start();
342 return 0;
344 #else
345 #define at32_wdt_suspend NULL
346 #define at32_wdt_resume NULL
347 #endif
349 static struct platform_driver at32_wdt_driver = {
350 .remove = __exit_p(at32_wdt_remove),
351 .suspend = at32_wdt_suspend,
352 .resume = at32_wdt_resume,
353 .driver = {
354 .name = "at32_wdt",
355 .owner = THIS_MODULE,
357 .shutdown = at32_wdt_shutdown,
360 static int __init at32_wdt_init(void)
362 return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
364 module_init(at32_wdt_init);
366 static void __exit at32_wdt_exit(void)
368 platform_driver_unregister(&at32_wdt_driver);
370 module_exit(at32_wdt_exit);
372 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
373 MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
374 MODULE_LICENSE("GPL");
375 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);