spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / watchdog / at91sam9_wdt.c
blob00562566ef5f2773b006c80a7981cb5da56c7867
1 /*
2 * Watchdog driver for Atmel AT91SAM9x processors.
4 * Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 * The Watchdog Timer Mode Register can be only written to once. If the
14 * timeout need to be set from Linux, be sure that the bootstrap or the
15 * bootloader doesn't write to this register.
18 #include <linux/errno.h>
19 #include <linux/fs.h>
20 #include <linux/init.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/miscdevice.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/platform_device.h>
27 #include <linux/types.h>
28 #include <linux/watchdog.h>
29 #include <linux/jiffies.h>
30 #include <linux/timer.h>
31 #include <linux/bitops.h>
32 #include <linux/uaccess.h>
34 #include "at91sam9_wdt.h"
36 #define DRV_NAME "AT91SAM9 Watchdog"
38 #define wdt_read(field) \
39 __raw_readl(at91wdt_private.base + field)
40 #define wdt_write(field, val) \
41 __raw_writel((val), at91wdt_private.base + field)
43 /* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
44 * use this to convert a watchdog
45 * value from/to milliseconds.
47 #define ms_to_ticks(t) (((t << 8) / 1000) - 1)
48 #define ticks_to_ms(t) (((t + 1) * 1000) >> 8)
50 /* Hardware timeout in seconds */
51 #define WDT_HW_TIMEOUT 2
53 /* Timer heartbeat (500ms) */
54 #define WDT_TIMEOUT (HZ/2)
56 /* User land timeout */
57 #define WDT_HEARTBEAT 15
58 static int heartbeat = WDT_HEARTBEAT;
59 module_param(heartbeat, int, 0);
60 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
61 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
63 static int nowayout = WATCHDOG_NOWAYOUT;
64 module_param(nowayout, int, 0);
65 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
66 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
68 static void at91_ping(unsigned long data);
70 static struct {
71 void __iomem *base;
72 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
73 unsigned long open;
74 char expect_close;
75 struct timer_list timer; /* The timer that pings the watchdog */
76 } at91wdt_private;
78 /* ......................................................................... */
82 * Reload the watchdog timer. (ie, pat the watchdog)
84 static inline void at91_wdt_reset(void)
86 wdt_write(AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
90 * Timer tick
92 static void at91_ping(unsigned long data)
94 if (time_before(jiffies, at91wdt_private.next_heartbeat) ||
95 (!nowayout && !at91wdt_private.open)) {
96 at91_wdt_reset();
97 mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
98 } else
99 printk(KERN_CRIT DRV_NAME": I will reset your machine !\n");
103 * Watchdog device is opened, and watchdog starts running.
105 static int at91_wdt_open(struct inode *inode, struct file *file)
107 if (test_and_set_bit(0, &at91wdt_private.open))
108 return -EBUSY;
110 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
111 mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
113 return nonseekable_open(inode, file);
117 * Close the watchdog device.
119 static int at91_wdt_close(struct inode *inode, struct file *file)
121 clear_bit(0, &at91wdt_private.open);
123 /* stop internal ping */
124 if (!at91wdt_private.expect_close)
125 del_timer(&at91wdt_private.timer);
127 at91wdt_private.expect_close = 0;
128 return 0;
132 * Set the watchdog time interval in 1/256Hz (write-once)
133 * Counter is 12 bit.
135 static int at91_wdt_settimeout(unsigned int timeout)
137 unsigned int reg;
138 unsigned int mr;
140 /* Check if disabled */
141 mr = wdt_read(AT91_WDT_MR);
142 if (mr & AT91_WDT_WDDIS) {
143 printk(KERN_ERR DRV_NAME": sorry, watchdog is disabled\n");
144 return -EIO;
148 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
150 * Since WDV is a 12-bit counter, the maximum period is
151 * 4096 / 256 = 16 seconds.
153 reg = AT91_WDT_WDRSTEN /* causes watchdog reset */
154 /* | AT91_WDT_WDRPROC causes processor reset only */
155 | AT91_WDT_WDDBGHLT /* disabled in debug mode */
156 | AT91_WDT_WDD /* restart at any time */
157 | (timeout & AT91_WDT_WDV); /* timer value */
158 wdt_write(AT91_WDT_MR, reg);
160 return 0;
163 static const struct watchdog_info at91_wdt_info = {
164 .identity = DRV_NAME,
165 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
166 WDIOF_MAGICCLOSE,
170 * Handle commands from user-space.
172 static long at91_wdt_ioctl(struct file *file,
173 unsigned int cmd, unsigned long arg)
175 void __user *argp = (void __user *)arg;
176 int __user *p = argp;
177 int new_value;
179 switch (cmd) {
180 case WDIOC_GETSUPPORT:
181 return copy_to_user(argp, &at91_wdt_info,
182 sizeof(at91_wdt_info)) ? -EFAULT : 0;
184 case WDIOC_GETSTATUS:
185 case WDIOC_GETBOOTSTATUS:
186 return put_user(0, p);
188 case WDIOC_KEEPALIVE:
189 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
190 return 0;
192 case WDIOC_SETTIMEOUT:
193 if (get_user(new_value, p))
194 return -EFAULT;
196 heartbeat = new_value;
197 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
199 return put_user(new_value, p); /* return current value */
201 case WDIOC_GETTIMEOUT:
202 return put_user(heartbeat, p);
204 return -ENOTTY;
208 * Pat the watchdog whenever device is written to.
210 static ssize_t at91_wdt_write(struct file *file, const char *data, size_t len,
211 loff_t *ppos)
213 if (!len)
214 return 0;
216 /* Scan for magic character */
217 if (!nowayout) {
218 size_t i;
220 at91wdt_private.expect_close = 0;
222 for (i = 0; i < len; i++) {
223 char c;
224 if (get_user(c, data + i))
225 return -EFAULT;
226 if (c == 'V') {
227 at91wdt_private.expect_close = 42;
228 break;
233 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
235 return len;
238 /* ......................................................................... */
240 static const struct file_operations at91wdt_fops = {
241 .owner = THIS_MODULE,
242 .llseek = no_llseek,
243 .unlocked_ioctl = at91_wdt_ioctl,
244 .open = at91_wdt_open,
245 .release = at91_wdt_close,
246 .write = at91_wdt_write,
249 static struct miscdevice at91wdt_miscdev = {
250 .minor = WATCHDOG_MINOR,
251 .name = "watchdog",
252 .fops = &at91wdt_fops,
255 static int __init at91wdt_probe(struct platform_device *pdev)
257 struct resource *r;
258 int res;
260 if (at91wdt_miscdev.parent)
261 return -EBUSY;
262 at91wdt_miscdev.parent = &pdev->dev;
264 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 if (!r)
266 return -ENODEV;
267 at91wdt_private.base = ioremap(r->start, resource_size(r));
268 if (!at91wdt_private.base) {
269 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
270 return -ENOMEM;
273 /* Set watchdog */
274 res = at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000));
275 if (res)
276 return res;
278 res = misc_register(&at91wdt_miscdev);
279 if (res)
280 return res;
282 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
283 setup_timer(&at91wdt_private.timer, at91_ping, 0);
284 mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
286 printk(KERN_INFO DRV_NAME " enabled (heartbeat=%d sec, nowayout=%d)\n",
287 heartbeat, nowayout);
289 return 0;
292 static int __exit at91wdt_remove(struct platform_device *pdev)
294 int res;
296 res = misc_deregister(&at91wdt_miscdev);
297 if (!res)
298 at91wdt_miscdev.parent = NULL;
300 return res;
303 static struct platform_driver at91wdt_driver = {
304 .remove = __exit_p(at91wdt_remove),
305 .driver = {
306 .name = "at91_wdt",
307 .owner = THIS_MODULE,
311 static int __init at91sam_wdt_init(void)
313 return platform_driver_probe(&at91wdt_driver, at91wdt_probe);
316 static void __exit at91sam_wdt_exit(void)
318 platform_driver_unregister(&at91wdt_driver);
321 module_init(at91sam_wdt_init);
322 module_exit(at91sam_wdt_exit);
324 MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
325 MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);