Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / watchdog / ks8695_wdt.c
blob0b798fdaa378eccd53e39c5ab1fa538e19388b03
1 /*
2 * Watchdog driver for Kendin/Micrel KS8695.
4 * (C) 2007 Andrew Victor
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/bitops.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/miscdevice.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/platform_device.h>
20 #include <linux/types.h>
21 #include <linux/watchdog.h>
22 #include <linux/io.h>
23 #include <linux/uaccess.h>
24 #include <mach/regs-timer.h>
26 #define WDT_DEFAULT_TIME 5 /* seconds */
27 #define WDT_MAX_TIME 171 /* seconds */
29 static int wdt_time = WDT_DEFAULT_TIME;
30 static int nowayout = WATCHDOG_NOWAYOUT;
32 module_param(wdt_time, int, 0);
33 MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
34 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
36 #ifdef CONFIG_WATCHDOG_NOWAYOUT
37 module_param(nowayout, int, 0);
38 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
39 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
40 #endif
43 static unsigned long ks8695wdt_busy;
44 static spinlock_t ks8695_lock;
46 /* ......................................................................... */
49 * Disable the watchdog.
51 static inline void ks8695_wdt_stop(void)
53 unsigned long tmcon;
55 spin_lock(&ks8695_lock);
56 /* disable timer0 */
57 tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
58 __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
59 spin_unlock(&ks8695_lock);
63 * Enable and reset the watchdog.
65 static inline void ks8695_wdt_start(void)
67 unsigned long tmcon;
68 unsigned long tval = wdt_time * CLOCK_TICK_RATE;
70 spin_lock(&ks8695_lock);
71 /* disable timer0 */
72 tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
73 __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
75 /* program timer0 */
76 __raw_writel(tval | T0TC_WATCHDOG, KS8695_TMR_VA + KS8695_T0TC);
78 /* re-enable timer0 */
79 tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
80 __raw_writel(tmcon | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
81 spin_unlock(&ks8695_lock);
85 * Reload the watchdog timer. (ie, pat the watchdog)
87 static inline void ks8695_wdt_reload(void)
89 unsigned long tmcon;
91 spin_lock(&ks8695_lock);
92 /* disable, then re-enable timer0 */
93 tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
94 __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
95 __raw_writel(tmcon | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
96 spin_unlock(&ks8695_lock);
100 * Change the watchdog time interval.
102 static int ks8695_wdt_settimeout(int new_time)
105 * All counting occurs at SLOW_CLOCK / 128 = 0.256 Hz
107 * Since WDV is a 16-bit counter, the maximum period is
108 * 65536 / 0.256 = 256 seconds.
110 if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
111 return -EINVAL;
113 /* Set new watchdog time. It will be used when
114 ks8695_wdt_start() is called. */
115 wdt_time = new_time;
116 return 0;
119 /* ......................................................................... */
122 * Watchdog device is opened, and watchdog starts running.
124 static int ks8695_wdt_open(struct inode *inode, struct file *file)
126 if (test_and_set_bit(0, &ks8695wdt_busy))
127 return -EBUSY;
129 ks8695_wdt_start();
130 return nonseekable_open(inode, file);
134 * Close the watchdog device.
135 * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
136 * disabled.
138 static int ks8695_wdt_close(struct inode *inode, struct file *file)
140 /* Disable the watchdog when file is closed */
141 if (!nowayout)
142 ks8695_wdt_stop();
143 clear_bit(0, &ks8695wdt_busy);
144 return 0;
147 static struct watchdog_info ks8695_wdt_info = {
148 .identity = "ks8695 watchdog",
149 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
153 * Handle commands from user-space.
155 static long ks8695_wdt_ioctl(struct file *file, unsigned int cmd,
156 unsigned long arg)
158 void __user *argp = (void __user *)arg;
159 int __user *p = argp;
160 int new_value;
162 switch (cmd) {
163 case WDIOC_GETSUPPORT:
164 return copy_to_user(argp, &ks8695_wdt_info,
165 sizeof(ks8695_wdt_info)) ? -EFAULT : 0;
166 case WDIOC_GETSTATUS:
167 case WDIOC_GETBOOTSTATUS:
168 return put_user(0, p);
169 case WDIOC_SETOPTIONS:
170 if (get_user(new_value, p))
171 return -EFAULT;
172 if (new_value & WDIOS_DISABLECARD)
173 ks8695_wdt_stop();
174 if (new_value & WDIOS_ENABLECARD)
175 ks8695_wdt_start();
176 return 0;
177 case WDIOC_KEEPALIVE:
178 ks8695_wdt_reload(); /* pat the watchdog */
179 return 0;
180 case WDIOC_SETTIMEOUT:
181 if (get_user(new_value, p))
182 return -EFAULT;
183 if (ks8695_wdt_settimeout(new_value))
184 return -EINVAL;
185 /* Enable new time value */
186 ks8695_wdt_start();
187 /* Return current value */
188 return put_user(wdt_time, p);
189 case WDIOC_GETTIMEOUT:
190 return put_user(wdt_time, p);
191 default:
192 return -ENOTTY;
197 * Pat the watchdog whenever device is written to.
199 static ssize_t ks8695_wdt_write(struct file *file, const char *data,
200 size_t len, loff_t *ppos)
202 ks8695_wdt_reload(); /* pat the watchdog */
203 return len;
206 /* ......................................................................... */
208 static const struct file_operations ks8695wdt_fops = {
209 .owner = THIS_MODULE,
210 .llseek = no_llseek,
211 .unlocked_ioctl = ks8695_wdt_ioctl,
212 .open = ks8695_wdt_open,
213 .release = ks8695_wdt_close,
214 .write = ks8695_wdt_write,
217 static struct miscdevice ks8695wdt_miscdev = {
218 .minor = WATCHDOG_MINOR,
219 .name = "watchdog",
220 .fops = &ks8695wdt_fops,
223 static int __init ks8695wdt_probe(struct platform_device *pdev)
225 int res;
227 if (ks8695wdt_miscdev.parent)
228 return -EBUSY;
229 ks8695wdt_miscdev.parent = &pdev->dev;
231 res = misc_register(&ks8695wdt_miscdev);
232 if (res)
233 return res;
235 printk(KERN_INFO "KS8695 Watchdog Timer enabled (%d seconds%s)\n",
236 wdt_time, nowayout ? ", nowayout" : "");
237 return 0;
240 static int __exit ks8695wdt_remove(struct platform_device *pdev)
242 int res;
244 res = misc_deregister(&ks8695wdt_miscdev);
245 if (!res)
246 ks8695wdt_miscdev.parent = NULL;
248 return res;
251 static void ks8695wdt_shutdown(struct platform_device *pdev)
253 ks8695_wdt_stop();
256 #ifdef CONFIG_PM
258 static int ks8695wdt_suspend(struct platform_device *pdev, pm_message_t message)
260 ks8695_wdt_stop();
261 return 0;
264 static int ks8695wdt_resume(struct platform_device *pdev)
266 if (ks8695wdt_busy)
267 ks8695_wdt_start();
268 return 0;
271 #else
272 #define ks8695wdt_suspend NULL
273 #define ks8695wdt_resume NULL
274 #endif
276 static struct platform_driver ks8695wdt_driver = {
277 .probe = ks8695wdt_probe,
278 .remove = __exit_p(ks8695wdt_remove),
279 .shutdown = ks8695wdt_shutdown,
280 .suspend = ks8695wdt_suspend,
281 .resume = ks8695wdt_resume,
282 .driver = {
283 .name = "ks8695_wdt",
284 .owner = THIS_MODULE,
288 static int __init ks8695_wdt_init(void)
290 spin_lock_init(&ks8695_lock);
291 /* Check that the heartbeat value is within range;
292 if not reset to the default */
293 if (ks8695_wdt_settimeout(wdt_time)) {
294 ks8695_wdt_settimeout(WDT_DEFAULT_TIME);
295 pr_info("ks8695_wdt: wdt_time value must be 1 <= wdt_time <= %i, using %d\n",
296 wdt_time, WDT_MAX_TIME);
298 return platform_driver_register(&ks8695wdt_driver);
301 static void __exit ks8695_wdt_exit(void)
303 platform_driver_unregister(&ks8695wdt_driver);
306 module_init(ks8695_wdt_init);
307 module_exit(ks8695_wdt_exit);
309 MODULE_AUTHOR("Andrew Victor");
310 MODULE_DESCRIPTION("Watchdog driver for KS8695");
311 MODULE_LICENSE("GPL");
312 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
313 MODULE_ALIAS("platform:ks8695_wdt");