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