ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / watchdog / alim7101_wdt.c
1 /*
2  *      ALi M7101 PMU Computer Watchdog Timer driver
3  *
4  *      Based on w83877f_wdt.c by Scott Jennings <linuxdrivers@oro.net>
5  *      and the Cobalt kernel WDT timer driver by Tim Hockin
6  *                                            <thockin@cobaltnet.com>
7  *
8  *      (c)2002 Steve Hill <steve@navaho.co.uk>
9  *
10  *  This WDT driver is different from most other Linux WDT
11  *  drivers in that the driver will ping the watchdog by itself,
12  *  because this particular WDT has a very short timeout (1.6
13  *  seconds) and it would be insane to count on any userspace
14  *  daemon always getting scheduled within that time frame.
15  */
16
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/types.h>
20 #include <linux/timer.h>
21 #include <linux/miscdevice.h>
22 #include <linux/watchdog.h>
23 #include <linux/ioport.h>
24 #include <linux/notifier.h>
25 #include <linux/reboot.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32
33 #define OUR_NAME "alim7101_wdt"
34 #define PFX OUR_NAME ": "
35
36 #define WDT_ENABLE 0x9C
37 #define WDT_DISABLE 0x8C
38
39 #define ALI_7101_WDT    0x92
40 #define ALI_WDT_ARM     0x01
41
42 /*
43  * We're going to use a 1 second timeout.
44  * If we reset the watchdog every ~250ms we should be safe.  */
45
46 #define WDT_INTERVAL (HZ/4+1)
47
48 /*
49  * We must not require too good response from the userspace daemon.
50  * Here we require the userspace daemon to send us a heartbeat
51  * char to /dev/watchdog every 30 seconds.
52  */
53
54 #define WATCHDOG_TIMEOUT 30            /* 30 sec default timeout */
55 static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
56 module_param(timeout, int, 0);
57 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
58
59 static void wdt_timer_ping(unsigned long);
60 static struct timer_list timer;
61 static unsigned long next_heartbeat;
62 static unsigned long wdt_is_open;
63 static char wdt_expect_close;
64 static struct pci_dev *alim7101_pmu;
65
66 #ifdef CONFIG_WATCHDOG_NOWAYOUT
67 static int nowayout = 1;
68 #else
69 static int nowayout = 0;
70 #endif
71
72 module_param(nowayout, int, 0);
73 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
74
75 /*
76  *      Whack the dog
77  */
78
79 static void wdt_timer_ping(unsigned long data)
80 {
81         /* If we got a heartbeat pulse within the WDT_US_INTERVAL
82          * we agree to ping the WDT
83          */
84         char    tmp;
85
86         if(time_before(jiffies, next_heartbeat))
87         {
88                 /* Ping the WDT (this is actually a disarm/arm sequence) */
89                 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
90                 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
91                 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
92         } else {
93                 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
94         }
95         /* Re-set the timer interval */
96         timer.expires = jiffies + WDT_INTERVAL;
97         add_timer(&timer);
98 }
99
100 /*
101  * Utility routines
102  */
103
104 static void wdt_change(int writeval)
105 {
106         char    tmp;
107
108         pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
109         if (writeval == WDT_ENABLE)
110                 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
111         else
112                 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
113 }
114
115 static void wdt_startup(void)
116 {
117         next_heartbeat = jiffies + (timeout * HZ);
118
119         /* We must enable before we kick off the timer in case the timer
120            occurs as we ping it */
121
122         wdt_change(WDT_ENABLE);
123
124         /* Start the timer */
125         timer.expires = jiffies + WDT_INTERVAL;
126         add_timer(&timer);
127
128
129         printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
130 }
131
132 static void wdt_turnoff(void)
133 {
134         /* Stop the timer */
135         del_timer_sync(&timer);
136         wdt_change(WDT_DISABLE);
137         printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
138 }
139
140 static void wdt_keepalive(void)
141 {
142         /* user land ping */
143         next_heartbeat = jiffies + (timeout * HZ);
144 }
145
146 /*
147  * /dev/watchdog handling
148  */
149
150 static ssize_t fop_write(struct file * file, const char * buf, size_t count, loff_t * ppos)
151 {
152         /* We can't seek */
153         if(ppos != &file->f_pos)
154                 return -ESPIPE;
155
156         /* See if we got the magic character 'V' and reload the timer */
157         if(count) {
158                 if (!nowayout) {
159                         size_t ofs;
160
161                         /* note: just in case someone wrote the magic character
162                          * five months ago... */
163                         wdt_expect_close = 0;
164
165                         /* now scan */
166                         for (ofs = 0; ofs != count; ofs++) {
167                                 char c;
168                                 if (get_user(c, buf+ofs))
169                                         return -EFAULT;
170                                 if (c == 'V')
171                                         wdt_expect_close = 42;
172                         }
173                 }
174                 /* someone wrote to us, we should restart timer */
175                 wdt_keepalive();
176         }
177         return count;
178 }
179
180 static int fop_open(struct inode * inode, struct file * file)
181 {
182         /* Just in case we're already talking to someone... */
183         if(test_and_set_bit(0, &wdt_is_open))
184                 return -EBUSY;
185         /* Good, fire up the show */
186         wdt_startup();
187         return 0;
188 }
189
190 static int fop_close(struct inode * inode, struct file * file)
191 {
192         if(wdt_expect_close == 42)
193                 wdt_turnoff();
194         else {
195                 /* wim: shouldn't there be a: del_timer(&timer); */
196                 printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
197         }
198         clear_bit(0, &wdt_is_open);
199         wdt_expect_close = 0;
200         return 0;
201 }
202
203 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
204 {
205         static struct watchdog_info ident =
206         {
207                 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
208                 .firmware_version = 1,
209                 .identity = "ALiM7101",
210         };
211
212         switch(cmd)
213         {
214                 case WDIOC_GETSUPPORT:
215                         return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
216                 case WDIOC_GETSTATUS:
217                 case WDIOC_GETBOOTSTATUS:
218                         return put_user(0, (int *)arg);
219                 case WDIOC_KEEPALIVE:
220                         wdt_keepalive();
221                         return 0;
222                 case WDIOC_SETOPTIONS:
223                 {
224                         int new_options, retval = -EINVAL;
225
226                         if(get_user(new_options, (int *)arg))
227                                 return -EFAULT;
228
229                         if(new_options & WDIOS_DISABLECARD) {
230                                 wdt_turnoff();
231                                 retval = 0;
232                         }
233
234                         if(new_options & WDIOS_ENABLECARD) {
235                                 wdt_startup();
236                                 retval = 0;
237                         }
238
239                         return retval;
240                 }
241                 case WDIOC_SETTIMEOUT:
242                 {
243                         int new_timeout;
244
245                         if(get_user(new_timeout, (int *)arg))
246                                 return -EFAULT;
247
248                         if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
249                                 return -EINVAL;
250
251                         timeout = new_timeout;
252                         wdt_keepalive();
253                         /* Fall through */
254                 }
255                 case WDIOC_GETTIMEOUT:
256                         return put_user(timeout, (int *)arg);
257                 default:
258                         return -ENOIOCTLCMD;
259         }
260 }
261
262 static struct file_operations wdt_fops = {
263         .owner=         THIS_MODULE,
264         .llseek=        no_llseek,
265         .write=         fop_write,
266         .open=          fop_open,
267         .release=       fop_close,
268         .ioctl=         fop_ioctl,
269 };
270
271 static struct miscdevice wdt_miscdev = {
272         .minor=WATCHDOG_MINOR,
273         .name="watchdog",
274         .fops=&wdt_fops,
275 };
276
277 /*
278  *      Notifier for system down
279  */
280
281 static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
282 {
283         if (code==SYS_DOWN || code==SYS_HALT)
284                 wdt_turnoff();
285
286         if (code==SYS_RESTART) {
287                 /*
288                  * Cobalt devices have no way of rebooting themselves other than
289                  * getting the watchdog to pull reset, so we restart the watchdog on
290                  * reboot with no heartbeat
291                  */
292                 wdt_change(WDT_ENABLE);
293                 printk(KERN_INFO PFX "Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second.\n");
294         }
295         return NOTIFY_DONE;
296 }
297
298 /*
299  *      The WDT needs to learn about soft shutdowns in order to
300  *      turn the timebomb registers off.
301  */
302
303 static struct notifier_block wdt_notifier=
304 {
305         .notifier_call = wdt_notify_sys,
306 };
307
308 static void __exit alim7101_wdt_unload(void)
309 {
310         wdt_turnoff();
311         /* Deregister */
312         misc_deregister(&wdt_miscdev);
313         unregister_reboot_notifier(&wdt_notifier);
314 }
315
316 static int __init alim7101_wdt_init(void)
317 {
318         int rc = -EBUSY;
319         struct pci_dev *ali1543_south;
320         char tmp;
321
322         printk(KERN_INFO PFX "Steve Hill <steve@navaho.co.uk>.\n");
323         alim7101_pmu = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,NULL);
324         if (!alim7101_pmu) {
325                 printk(KERN_INFO PFX "ALi M7101 PMU not present - WDT not set\n");
326                 return -EBUSY;
327         }
328
329         /* Set the WDT in the PMU to 1 second */
330         pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
331
332         ali1543_south = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
333         if (!ali1543_south) {
334                 printk(KERN_INFO PFX "ALi 1543 South-Bridge not present - WDT not set\n");
335                 return -EBUSY;
336         }
337         pci_read_config_byte(ali1543_south, 0x5e, &tmp);
338         if ((tmp & 0x1e) != 0x12) {
339                 printk(KERN_INFO PFX "ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
340                 return -EBUSY;
341         }
342
343         if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
344         {
345                 timeout = WATCHDOG_TIMEOUT;
346                 printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
347                         timeout);
348         }
349
350         init_timer(&timer);
351         timer.function = wdt_timer_ping;
352         timer.data = 1;
353
354         rc = misc_register(&wdt_miscdev);
355         if (rc) {
356                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
357                         wdt_miscdev.minor, rc);
358                 goto err_out;
359         }
360
361         rc = register_reboot_notifier(&wdt_notifier);
362         if (rc) {
363                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
364                         rc);
365                 goto err_out_miscdev;
366         }
367
368         printk(KERN_INFO PFX "WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
369                 timeout, nowayout);
370         return 0;
371
372 err_out_miscdev:
373         misc_deregister(&wdt_miscdev);
374 err_out:
375         return rc;
376 }
377
378 module_init(alim7101_wdt_init);
379 module_exit(alim7101_wdt_unload);
380
381 MODULE_AUTHOR("Steve Hill");
382 MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
383 MODULE_LICENSE("GPL");
384 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);