blob: 0b798fdaa378eccd53e39c5ab1fa538e19388b03 [file] [log] [blame]
Andrew Victorccb8f432007-05-14 14:45:25 +02001/*
2 * Watchdog driver for Kendin/Micrel KS8695.
3 *
4 * (C) 2007 Andrew Victor
5 *
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 */
10
Jiri Slaby1977f032007-10-18 23:40:25 -070011#include <linux/bitops.h>
Andrew Victorccb8f432007-05-14 14:45:25 +020012#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>
Alan Coxf4fabce2008-05-19 14:06:53 +010022#include <linux/io.h>
23#include <linux/uaccess.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/regs-timer.h>
Andrew Victorccb8f432007-05-14 14:45:25 +020025
Andrew Victorccb8f432007-05-14 14:45:25 +020026#define WDT_DEFAULT_TIME 5 /* seconds */
27#define WDT_MAX_TIME 171 /* seconds */
28
29static int wdt_time = WDT_DEFAULT_TIME;
30static int nowayout = WATCHDOG_NOWAYOUT;
31
32module_param(wdt_time, int, 0);
Alan Coxf4fabce2008-05-19 14:06:53 +010033MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
34 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
Andrew Victorccb8f432007-05-14 14:45:25 +020035
36#ifdef CONFIG_WATCHDOG_NOWAYOUT
37module_param(nowayout, int, 0);
Alan Coxf4fabce2008-05-19 14:06:53 +010038MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
39 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Andrew Victorccb8f432007-05-14 14:45:25 +020040#endif
41
42
43static unsigned long ks8695wdt_busy;
Alan Coxf4fabce2008-05-19 14:06:53 +010044static spinlock_t ks8695_lock;
Andrew Victorccb8f432007-05-14 14:45:25 +020045
46/* ......................................................................... */
47
48/*
49 * Disable the watchdog.
50 */
Alan Coxf4fabce2008-05-19 14:06:53 +010051static inline void ks8695_wdt_stop(void)
Andrew Victorccb8f432007-05-14 14:45:25 +020052{
53 unsigned long tmcon;
54
Alan Coxf4fabce2008-05-19 14:06:53 +010055 spin_lock(&ks8695_lock);
Andrew Victorccb8f432007-05-14 14:45:25 +020056 /* disable timer0 */
57 tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
58 __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
Alan Coxf4fabce2008-05-19 14:06:53 +010059 spin_unlock(&ks8695_lock);
Andrew Victorccb8f432007-05-14 14:45:25 +020060}
61
62/*
63 * Enable and reset the watchdog.
64 */
Alan Coxf4fabce2008-05-19 14:06:53 +010065static inline void ks8695_wdt_start(void)
Andrew Victorccb8f432007-05-14 14:45:25 +020066{
67 unsigned long tmcon;
68 unsigned long tval = wdt_time * CLOCK_TICK_RATE;
69
Alan Coxf4fabce2008-05-19 14:06:53 +010070 spin_lock(&ks8695_lock);
Andrew Victorccb8f432007-05-14 14:45:25 +020071 /* disable timer0 */
72 tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
73 __raw_writel(tmcon & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
74
75 /* program timer0 */
76 __raw_writel(tval | T0TC_WATCHDOG, KS8695_TMR_VA + KS8695_T0TC);
77
78 /* re-enable timer0 */
79 tmcon = __raw_readl(KS8695_TMR_VA + KS8695_TMCON);
80 __raw_writel(tmcon | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
Alan Coxf4fabce2008-05-19 14:06:53 +010081 spin_unlock(&ks8695_lock);
Andrew Victorccb8f432007-05-14 14:45:25 +020082}
83
84/*
85 * Reload the watchdog timer. (ie, pat the watchdog)
86 */
Alan Coxf4fabce2008-05-19 14:06:53 +010087static inline void ks8695_wdt_reload(void)
Andrew Victorccb8f432007-05-14 14:45:25 +020088{
89 unsigned long tmcon;
90
Alan Coxf4fabce2008-05-19 14:06:53 +010091 spin_lock(&ks8695_lock);
Andrew Victorccb8f432007-05-14 14:45:25 +020092 /* 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);
Alan Coxf4fabce2008-05-19 14:06:53 +010096 spin_unlock(&ks8695_lock);
Andrew Victorccb8f432007-05-14 14:45:25 +020097}
98
99/*
100 * Change the watchdog time interval.
101 */
102static int ks8695_wdt_settimeout(int new_time)
103{
104 /*
105 * All counting occurs at SLOW_CLOCK / 128 = 0.256 Hz
106 *
107 * Since WDV is a 16-bit counter, the maximum period is
108 * 65536 / 0.256 = 256 seconds.
109 */
110 if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
111 return -EINVAL;
112
Alan Coxf4fabce2008-05-19 14:06:53 +0100113 /* Set new watchdog time. It will be used when
114 ks8695_wdt_start() is called. */
Andrew Victorccb8f432007-05-14 14:45:25 +0200115 wdt_time = new_time;
116 return 0;
117}
118
119/* ......................................................................... */
120
121/*
122 * Watchdog device is opened, and watchdog starts running.
123 */
124static int ks8695_wdt_open(struct inode *inode, struct file *file)
125{
126 if (test_and_set_bit(0, &ks8695wdt_busy))
127 return -EBUSY;
128
129 ks8695_wdt_start();
130 return nonseekable_open(inode, file);
131}
132
133/*
134 * Close the watchdog device.
135 * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
136 * disabled.
137 */
138static int ks8695_wdt_close(struct inode *inode, struct file *file)
139{
Alan Coxf4fabce2008-05-19 14:06:53 +0100140 /* Disable the watchdog when file is closed */
Andrew Victorccb8f432007-05-14 14:45:25 +0200141 if (!nowayout)
Alan Coxf4fabce2008-05-19 14:06:53 +0100142 ks8695_wdt_stop();
Andrew Victorccb8f432007-05-14 14:45:25 +0200143 clear_bit(0, &ks8695wdt_busy);
144 return 0;
145}
146
147static struct watchdog_info ks8695_wdt_info = {
148 .identity = "ks8695 watchdog",
149 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
150};
151
152/*
153 * Handle commands from user-space.
154 */
Alan Coxf4fabce2008-05-19 14:06:53 +0100155static long ks8695_wdt_ioctl(struct file *file, unsigned int cmd,
156 unsigned long arg)
Andrew Victorccb8f432007-05-14 14:45:25 +0200157{
158 void __user *argp = (void __user *)arg;
159 int __user *p = argp;
160 int new_value;
161
Alan Coxf4fabce2008-05-19 14:06:53 +0100162 switch (cmd) {
Alan Coxf4fabce2008-05-19 14:06:53 +0100163 case WDIOC_GETSUPPORT:
164 return copy_to_user(argp, &ks8695_wdt_info,
165 sizeof(ks8695_wdt_info)) ? -EFAULT : 0;
Alan Coxf4fabce2008-05-19 14:06:53 +0100166 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)
Andrew Victorccb8f432007-05-14 14:45:25 +0200175 ks8695_wdt_start();
Alan Coxf4fabce2008-05-19 14:06:53 +0100176 return 0;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000177 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);
Alan Coxf4fabce2008-05-19 14:06:53 +0100191 default:
192 return -ENOTTY;
Andrew Victorccb8f432007-05-14 14:45:25 +0200193 }
194}
195
196/*
197 * Pat the watchdog whenever device is written to.
198 */
Alan Coxf4fabce2008-05-19 14:06:53 +0100199static ssize_t ks8695_wdt_write(struct file *file, const char *data,
200 size_t len, loff_t *ppos)
Andrew Victorccb8f432007-05-14 14:45:25 +0200201{
202 ks8695_wdt_reload(); /* pat the watchdog */
203 return len;
204}
205
206/* ......................................................................... */
207
208static const struct file_operations ks8695wdt_fops = {
209 .owner = THIS_MODULE,
210 .llseek = no_llseek,
Alan Coxf4fabce2008-05-19 14:06:53 +0100211 .unlocked_ioctl = ks8695_wdt_ioctl,
Andrew Victorccb8f432007-05-14 14:45:25 +0200212 .open = ks8695_wdt_open,
213 .release = ks8695_wdt_close,
214 .write = ks8695_wdt_write,
215};
216
217static struct miscdevice ks8695wdt_miscdev = {
218 .minor = WATCHDOG_MINOR,
219 .name = "watchdog",
220 .fops = &ks8695wdt_fops,
221};
222
223static int __init ks8695wdt_probe(struct platform_device *pdev)
224{
225 int res;
226
227 if (ks8695wdt_miscdev.parent)
228 return -EBUSY;
229 ks8695wdt_miscdev.parent = &pdev->dev;
230
231 res = misc_register(&ks8695wdt_miscdev);
232 if (res)
233 return res;
234
Alan Coxf4fabce2008-05-19 14:06:53 +0100235 printk(KERN_INFO "KS8695 Watchdog Timer enabled (%d seconds%s)\n",
236 wdt_time, nowayout ? ", nowayout" : "");
Andrew Victorccb8f432007-05-14 14:45:25 +0200237 return 0;
238}
239
240static int __exit ks8695wdt_remove(struct platform_device *pdev)
241{
242 int res;
243
244 res = misc_deregister(&ks8695wdt_miscdev);
245 if (!res)
246 ks8695wdt_miscdev.parent = NULL;
247
248 return res;
249}
250
251static void ks8695wdt_shutdown(struct platform_device *pdev)
252{
253 ks8695_wdt_stop();
254}
255
256#ifdef CONFIG_PM
257
258static int ks8695wdt_suspend(struct platform_device *pdev, pm_message_t message)
259{
260 ks8695_wdt_stop();
261 return 0;
262}
263
264static int ks8695wdt_resume(struct platform_device *pdev)
265{
266 if (ks8695wdt_busy)
267 ks8695_wdt_start();
268 return 0;
269}
270
271#else
272#define ks8695wdt_suspend NULL
273#define ks8695wdt_resume NULL
274#endif
275
276static 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,
285 },
286};
287
288static int __init ks8695_wdt_init(void)
289{
Alan Coxf4fabce2008-05-19 14:06:53 +0100290 spin_lock_init(&ks8695_lock);
291 /* Check that the heartbeat value is within range;
292 if not reset to the default */
Andrew Victorccb8f432007-05-14 14:45:25 +0200293 if (ks8695_wdt_settimeout(wdt_time)) {
294 ks8695_wdt_settimeout(WDT_DEFAULT_TIME);
Alan Coxf4fabce2008-05-19 14:06:53 +0100295 pr_info("ks8695_wdt: wdt_time value must be 1 <= wdt_time <= %i, using %d\n",
296 wdt_time, WDT_MAX_TIME);
Andrew Victorccb8f432007-05-14 14:45:25 +0200297 }
Andrew Victorccb8f432007-05-14 14:45:25 +0200298 return platform_driver_register(&ks8695wdt_driver);
299}
300
301static void __exit ks8695_wdt_exit(void)
302{
303 platform_driver_unregister(&ks8695wdt_driver);
304}
305
306module_init(ks8695_wdt_init);
307module_exit(ks8695_wdt_exit);
308
309MODULE_AUTHOR("Andrew Victor");
310MODULE_DESCRIPTION("Watchdog driver for KS8695");
311MODULE_LICENSE("GPL");
312MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700313MODULE_ALIAS("platform:ks8695_wdt");