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