Linux 3.4.102
[linux/fpc-iii.git] / drivers / watchdog / at91sam9_wdt.c
blob05e1be85fdeeda07a21651ee1a4096bf6b77c06d
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/errno.h>
21 #include <linux/fs.h>
22 #include <linux/init.h>
23 #include <linux/io.h>
24 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/platform_device.h>
29 #include <linux/types.h>
30 #include <linux/watchdog.h>
31 #include <linux/jiffies.h>
32 #include <linux/timer.h>
33 #include <linux/bitops.h>
34 #include <linux/uaccess.h>
36 #include "at91sam9_wdt.h"
38 #define DRV_NAME "AT91SAM9 Watchdog"
40 #define wdt_read(field) \
41 __raw_readl(at91wdt_private.base + field)
42 #define wdt_write(field, val) \
43 __raw_writel((val), at91wdt_private.base + field)
45 /* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
46 * use this to convert a watchdog
47 * value from/to milliseconds.
49 #define ms_to_ticks(t) (((t << 8) / 1000) - 1)
50 #define ticks_to_ms(t) (((t + 1) * 1000) >> 8)
52 /* Hardware timeout in seconds */
53 #define WDT_HW_TIMEOUT 2
55 /* Timer heartbeat (500ms) */
56 #define WDT_TIMEOUT (HZ/2)
58 /* User land timeout */
59 #define WDT_HEARTBEAT 15
60 static int heartbeat = WDT_HEARTBEAT;
61 module_param(heartbeat, int, 0);
62 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
63 "(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
65 static bool nowayout = WATCHDOG_NOWAYOUT;
66 module_param(nowayout, bool, 0);
67 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
68 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
70 static void at91_ping(unsigned long data);
72 static struct {
73 void __iomem *base;
74 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
75 unsigned long open;
76 char expect_close;
77 struct timer_list timer; /* The timer that pings the watchdog */
78 } at91wdt_private;
80 /* ......................................................................... */
84 * Reload the watchdog timer. (ie, pat the watchdog)
86 static inline void at91_wdt_reset(void)
88 wdt_write(AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
92 * Timer tick
94 static void at91_ping(unsigned long data)
96 if (time_before(jiffies, at91wdt_private.next_heartbeat) ||
97 (!nowayout && !at91wdt_private.open)) {
98 at91_wdt_reset();
99 mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
100 } else
101 pr_crit("I will reset your machine !\n");
105 * Watchdog device is opened, and watchdog starts running.
107 static int at91_wdt_open(struct inode *inode, struct file *file)
109 if (test_and_set_bit(0, &at91wdt_private.open))
110 return -EBUSY;
112 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
113 mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
115 return nonseekable_open(inode, file);
119 * Close the watchdog device.
121 static int at91_wdt_close(struct inode *inode, struct file *file)
123 clear_bit(0, &at91wdt_private.open);
125 /* stop internal ping */
126 if (!at91wdt_private.expect_close)
127 del_timer(&at91wdt_private.timer);
129 at91wdt_private.expect_close = 0;
130 return 0;
134 * Set the watchdog time interval in 1/256Hz (write-once)
135 * Counter is 12 bit.
137 static int at91_wdt_settimeout(unsigned int timeout)
139 unsigned int reg;
140 unsigned int mr;
142 /* Check if disabled */
143 mr = wdt_read(AT91_WDT_MR);
144 if (mr & AT91_WDT_WDDIS) {
145 pr_err("sorry, watchdog is disabled\n");
146 return -EIO;
150 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
152 * Since WDV is a 12-bit counter, the maximum period is
153 * 4096 / 256 = 16 seconds.
155 reg = AT91_WDT_WDRSTEN /* causes watchdog reset */
156 /* | AT91_WDT_WDRPROC causes processor reset only */
157 | AT91_WDT_WDDBGHLT /* disabled in debug mode */
158 | AT91_WDT_WDD /* restart at any time */
159 | (timeout & AT91_WDT_WDV); /* timer value */
160 wdt_write(AT91_WDT_MR, reg);
162 return 0;
165 static const struct watchdog_info at91_wdt_info = {
166 .identity = DRV_NAME,
167 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
168 WDIOF_MAGICCLOSE,
172 * Handle commands from user-space.
174 static long at91_wdt_ioctl(struct file *file,
175 unsigned int cmd, unsigned long arg)
177 void __user *argp = (void __user *)arg;
178 int __user *p = argp;
179 int new_value;
181 switch (cmd) {
182 case WDIOC_GETSUPPORT:
183 return copy_to_user(argp, &at91_wdt_info,
184 sizeof(at91_wdt_info)) ? -EFAULT : 0;
186 case WDIOC_GETSTATUS:
187 case WDIOC_GETBOOTSTATUS:
188 return put_user(0, p);
190 case WDIOC_KEEPALIVE:
191 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
192 return 0;
194 case WDIOC_SETTIMEOUT:
195 if (get_user(new_value, p))
196 return -EFAULT;
198 heartbeat = new_value;
199 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
201 return put_user(new_value, p); /* return current value */
203 case WDIOC_GETTIMEOUT:
204 return put_user(heartbeat, p);
206 return -ENOTTY;
210 * Pat the watchdog whenever device is written to.
212 static ssize_t at91_wdt_write(struct file *file, const char *data, size_t len,
213 loff_t *ppos)
215 if (!len)
216 return 0;
218 /* Scan for magic character */
219 if (!nowayout) {
220 size_t i;
222 at91wdt_private.expect_close = 0;
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 at91wdt_private.expect_close = 42;
230 break;
235 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
237 return len;
240 /* ......................................................................... */
242 static const struct file_operations at91wdt_fops = {
243 .owner = THIS_MODULE,
244 .llseek = no_llseek,
245 .unlocked_ioctl = at91_wdt_ioctl,
246 .open = at91_wdt_open,
247 .release = at91_wdt_close,
248 .write = at91_wdt_write,
251 static struct miscdevice at91wdt_miscdev = {
252 .minor = WATCHDOG_MINOR,
253 .name = "watchdog",
254 .fops = &at91wdt_fops,
257 static int __init at91wdt_probe(struct platform_device *pdev)
259 struct resource *r;
260 int res;
262 if (at91wdt_miscdev.parent)
263 return -EBUSY;
264 at91wdt_miscdev.parent = &pdev->dev;
266 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267 if (!r)
268 return -ENODEV;
269 at91wdt_private.base = ioremap(r->start, resource_size(r));
270 if (!at91wdt_private.base) {
271 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
272 return -ENOMEM;
275 /* Set watchdog */
276 res = at91_wdt_settimeout(ms_to_ticks(WDT_HW_TIMEOUT * 1000));
277 if (res)
278 return res;
280 res = misc_register(&at91wdt_miscdev);
281 if (res)
282 return res;
284 at91wdt_private.next_heartbeat = jiffies + heartbeat * HZ;
285 setup_timer(&at91wdt_private.timer, at91_ping, 0);
286 mod_timer(&at91wdt_private.timer, jiffies + WDT_TIMEOUT);
288 pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n",
289 heartbeat, nowayout);
291 return 0;
294 static int __exit at91wdt_remove(struct platform_device *pdev)
296 int res;
298 res = misc_deregister(&at91wdt_miscdev);
299 if (!res)
300 at91wdt_miscdev.parent = NULL;
302 return res;
305 static struct platform_driver at91wdt_driver = {
306 .remove = __exit_p(at91wdt_remove),
307 .driver = {
308 .name = "at91_wdt",
309 .owner = THIS_MODULE,
313 static int __init at91sam_wdt_init(void)
315 return platform_driver_probe(&at91wdt_driver, at91wdt_probe);
318 static void __exit at91sam_wdt_exit(void)
320 platform_driver_unregister(&at91wdt_driver);
323 module_init(at91sam_wdt_init);
324 module_exit(at91sam_wdt_exit);
326 MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
327 MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
328 MODULE_LICENSE("GPL");
329 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);