patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / watchdog / alim1535_wdt.c
1 /*
2  *      Watchdog for the 7101 PMU version found in the ALi M1535 chipsets
3  *
4  *      This program is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU General Public License
6  *      as published by the Free Software Foundation; either version
7  *      2 of the License, or (at your option) any later version.
8  */
9
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/types.h>
13 #include <linux/miscdevice.h>
14 #include <linux/watchdog.h>
15 #include <linux/ioport.h>
16 #include <linux/notifier.h>
17 #include <linux/reboot.h>
18 #include <linux/init.h>
19 #include <linux/fs.h>
20 #include <linux/pci.h>
21
22 #include <asm/uaccess.h>
23 #include <asm/io.h>
24
25 #define WATCHDOG_NAME "ALi_M1535"
26 #define PFX WATCHDOG_NAME ": "
27 #define WATCHDOG_TIMEOUT 60     /* 60 sec default timeout */
28
29 /* internal variables */
30 static unsigned long ali_is_open;
31 static char ali_expect_release;
32 static struct pci_dev *ali_pci;
33 static u32 ali_timeout_bits;    /* stores the computed timeout */
34 static spinlock_t ali_lock;     /* Guards the hardware */
35
36 /* module parameters */
37 static int timeout = WATCHDOG_TIMEOUT;
38 module_param(timeout, int, 0);
39 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (0<timeout<18000, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
40
41 #ifdef CONFIG_WATCHDOG_NOWAYOUT
42 static int nowayout = 1;
43 #else
44 static int nowayout = 0;
45 #endif
46
47 module_param(nowayout, int, 0);
48 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
49
50 /*
51  *      ali_start       -       start watchdog countdown
52  *
53  *      Starts the timer running providing the timer has a counter
54  *      configuration set.
55  */
56
57 static void ali_start(void)
58 {
59         u32 val;
60
61         spin_lock(&ali_lock);
62
63         pci_read_config_dword(ali_pci, 0xCC, &val);
64         val &= ~0x3F;   /* Mask count */
65         val |= (1<<25) | ali_timeout_bits;
66         pci_write_config_dword(ali_pci, 0xCC, val);
67
68         spin_unlock(&ali_lock);
69 }
70
71 /*
72  *      ali_stop        -       stop the timer countdown
73  *
74  *      Stop the ALi watchdog countdown
75  */
76
77 static void ali_stop(void)
78 {
79         u32 val;
80
81         spin_lock(&ali_lock);
82
83         pci_read_config_dword(ali_pci, 0xCC, &val);
84         val &= ~0x3F;   /* Mask count to zero (disabled) */
85         val &= ~(1<<25);/* and for safety mask the reset enable */
86         pci_write_config_dword(ali_pci, 0xCC, val);
87
88         spin_unlock(&ali_lock);
89 }
90
91 /*
92  *      ali_keepalive   -       send a keepalive to the watchdog
93  *
94  *      Send a keepalive to the timer (actually we restart the timer).
95  */
96
97 static void ali_keepalive(void)
98 {
99         ali_start();
100 }
101
102 /*
103  *      ali_settimer    -       compute the timer reload value
104  *      @t: time in seconds
105  *
106  *      Computes the timeout values needed
107  */
108
109 static int ali_settimer(int t)
110 {
111         if(t < 0)
112                 return -EINVAL;
113         else if(t < 60)
114                 ali_timeout_bits = t|(1<<6);
115         else if(t < 3600)
116                 ali_timeout_bits = (t/60)|(1<<7);
117         else if(t < 18000)
118                 ali_timeout_bits = (t/300)|(1<<6)|(1<<7);
119         else return -EINVAL;
120
121         timeout = t;
122         return 0;
123 }
124
125 /*
126  *      /dev/watchdog handling
127  */
128
129 /*
130  *      ali_write       -       writes to ALi watchdog
131  *      @file: file from VFS
132  *      @data: user address of data
133  *      @len: length of data
134  *      @ppos: pointer to the file offset
135  *
136  *      Handle a write to the ALi watchdog. Writing to the file pings
137  *      the watchdog and resets it. Writing the magic 'V' sequence allows
138  *      the next close to turn off the watchdog.
139  */
140
141 static ssize_t ali_write(struct file *file, const char *data,
142                               size_t len, loff_t * ppos)
143 {
144         /*  Can't seek (pwrite) on this device  */
145         if (ppos != &file->f_pos)
146                 return -ESPIPE;
147
148         /* See if we got the magic character 'V' and reload the timer */
149         if (len) {
150                 if (!nowayout) {
151                         size_t i;
152
153                         /* note: just in case someone wrote the magic character
154                          * five months ago... */
155                         ali_expect_release = 0;
156
157                         /* scan to see whether or not we got the magic character */
158                         for (i = 0; i != len; i++) {
159                                 char c;
160                                 if(get_user(c, data+i))
161                                         return -EFAULT;
162                                 if (c == 'V')
163                                         ali_expect_release = 42;
164                         }
165                 }
166
167                 /* someone wrote to us, we should reload the timer */
168                 ali_start();
169         }
170         return len;
171 }
172
173 /*
174  *      ali_ioctl       -       handle watchdog ioctls
175  *      @inode: VFS inode
176  *      @file: VFS file pointer
177  *      @cmd: ioctl number
178  *      @arg: arguments to the ioctl
179  *
180  *      Handle the watchdog ioctls supported by the ALi driver. Really
181  *      we want an extension to enable irq ack monitoring and the like
182  */
183
184 static int ali_ioctl(struct inode *inode, struct file *file,
185                           unsigned int cmd, unsigned long arg)
186 {
187         static struct watchdog_info ident = {
188                 .options =              WDIOF_KEEPALIVEPING |
189                                         WDIOF_SETTIMEOUT |
190                                         WDIOF_MAGICCLOSE,
191                 .firmware_version =     0,
192                 .identity =             "ALi M1535 WatchDog Timer",
193         };
194
195         switch (cmd) {
196                 case WDIOC_GETSUPPORT:
197                         return copy_to_user((struct watchdog_info *) arg, &ident,
198                                 sizeof (ident)) ? -EFAULT : 0;
199
200                 case WDIOC_GETSTATUS:
201                 case WDIOC_GETBOOTSTATUS:
202                         return put_user(0, (int *) arg);
203
204                 case WDIOC_KEEPALIVE:
205                         ali_keepalive();
206                         return 0;
207
208                 case WDIOC_SETOPTIONS:
209                 {
210                         int new_options, retval = -EINVAL;
211
212                         if (get_user (new_options, (int *) arg))
213                                 return -EFAULT;
214
215                         if (new_options & WDIOS_DISABLECARD) {
216                                 ali_stop();
217                                 retval = 0;
218                         }
219
220                         if (new_options & WDIOS_ENABLECARD) {
221                                 ali_start();
222                                 retval = 0;
223                         }
224
225                         return retval;
226                 }
227
228                 case WDIOC_SETTIMEOUT:
229                 {
230                         int new_timeout;
231
232                         if (get_user(new_timeout, (int *) arg))
233                                 return -EFAULT;
234
235                         if (ali_settimer(new_timeout))
236                             return -EINVAL;
237
238                         ali_keepalive();
239                         /* Fall */
240                 }
241
242                 case WDIOC_GETTIMEOUT:
243                         return put_user(timeout, (int *)arg);
244
245                 default:
246                         return -ENOIOCTLCMD;
247         }
248 }
249
250 /*
251  *      ali_open        -       handle open of ali watchdog
252  *      @inode: inode from VFS
253  *      @file: file from VFS
254  *
255  *      Open the ALi watchdog device. Ensure only one person opens it
256  *      at a time. Also start the watchdog running.
257  */
258
259 static int ali_open(struct inode *inode, struct file *file)
260 {
261         /* /dev/watchdog can only be opened once */
262         if (test_and_set_bit(0, &ali_is_open))
263                 return -EBUSY;
264
265         /* Activate */
266         ali_start();
267         return 0;
268 }
269
270 /*
271  *      ali_release     -       close an ALi watchdog
272  *      @inode: inode from VFS
273  *      @file: file from VFS
274  *
275  *      Close the ALi watchdog device. Actual shutdown of the timer
276  *      only occurs if the magic sequence has been set.
277  */
278
279 static int ali_release(struct inode *inode, struct file *file)
280 {
281         /*
282          *      Shut off the timer.
283          */
284         if (ali_expect_release == 42) {
285                 ali_stop();
286         } else {
287                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
288                 ali_keepalive();
289         }
290         clear_bit(0, &ali_is_open);
291         ali_expect_release = 0;
292         return 0;
293 }
294
295 /*
296  *      ali_notify_sys  -       System down notifier
297  *
298  *      Notifier for system down
299  */
300
301
302 static int ali_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
303 {
304         if (code==SYS_DOWN || code==SYS_HALT) {
305                 /* Turn the WDT off */
306                 ali_stop();
307         }
308
309         return NOTIFY_DONE;
310 }
311
312 /*
313  *      Data for PCI driver interface
314  *
315  *      This data only exists for exporting the supported
316  *      PCI ids via MODULE_DEVICE_TABLE.  We do not actually
317  *      register a pci_driver, because someone else might one day
318  *      want to register another driver on the same PCI id.
319  */
320
321 static struct pci_device_id ali_pci_tbl[] = {
322         { PCI_VENDOR_ID_AL, 1535, PCI_ANY_ID, PCI_ANY_ID,},
323         { 0, },
324 };
325 MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
326
327 /*
328  *      ali_find_watchdog       -       find a 1535 and 7101
329  *
330  *      Scans the PCI hardware for a 1535 series bridge and matching 7101
331  *      watchdog device. This may be overtight but it is better to be safe
332  */
333
334 static int __init ali_find_watchdog(void)
335 {
336         struct pci_dev *pdev;
337         u32 wdog;
338
339         /* Check for a 1535 series bridge */
340         pdev = pci_find_device(PCI_VENDOR_ID_AL, 0x1535, NULL);
341         if(pdev == NULL)
342                 return -ENODEV;
343
344         /* Check for the a 7101 PMU */
345         pdev = pci_find_device(PCI_VENDOR_ID_AL, 0x7101, NULL);
346         if(pdev == NULL)
347                 return -ENODEV;
348
349         if(pci_enable_device(pdev))
350                 return -EIO;
351
352         ali_pci = pdev;
353
354         /*
355          *      Initialize the timer bits
356          */
357         pci_read_config_dword(pdev, 0xCC, &wdog);
358
359         wdog &= ~0x3F;          /* Timer bits */
360         wdog &= ~((1<<27)|(1<<26)|(1<<25)|(1<<24));     /* Issued events */
361         wdog &= ~((1<<16)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9));      /* No monitor bits */
362
363         pci_write_config_dword(pdev, 0xCC, wdog);
364
365         return 0;
366 }
367
368 /*
369  *      Kernel Interfaces
370  */
371
372 static struct file_operations ali_fops = {
373         .owner =        THIS_MODULE,
374         .llseek =       no_llseek,
375         .write =        ali_write,
376         .ioctl =        ali_ioctl,
377         .open =         ali_open,
378         .release =      ali_release,
379 };
380
381 static struct miscdevice ali_miscdev = {
382         .minor =        WATCHDOG_MINOR,
383         .name =         "watchdog",
384         .fops =         &ali_fops,
385 };
386
387 static struct notifier_block ali_notifier = {
388         .notifier_call =        ali_notify_sys,
389 };
390
391 /*
392  *      watchdog_init   -       module initialiser
393  *
394  *      Scan for a suitable watchdog and if so initialize it. Return an error
395  *      if we cannot, the error causes the module to unload
396  */
397
398 static int __init watchdog_init(void)
399 {
400         int ret;
401
402         spin_lock_init(&ali_lock);
403
404         /* Check whether or not the hardware watchdog is there */
405         if (ali_find_watchdog() != 0) {
406                 return -ENODEV;
407         }
408
409         /* Check that the timeout value is within it's range ; if not reset to the default */
410         if (timeout < 1 || timeout >= 18000) {
411                 timeout = WATCHDOG_TIMEOUT;
412                 printk(KERN_INFO PFX "timeout value must be 0<timeout<18000, using %d\n",
413                         timeout);
414         }
415
416         /* Calculate the watchdog's timeout */
417         ali_settimer(timeout);
418
419         ret = misc_register(&ali_miscdev);
420         if (ret != 0) {
421                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
422                         WATCHDOG_MINOR, ret);
423                 goto out;
424         }
425
426         ret = register_reboot_notifier(&ali_notifier);
427         if (ret != 0) {
428                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
429                         ret);
430                 goto unreg_miscdev;
431         }
432
433         printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
434                 timeout, nowayout);
435
436 out:
437         return ret;
438 unreg_miscdev:
439         misc_deregister(&ali_miscdev);
440         goto out;
441 }
442
443 /*
444  *      watchdog_exit   -       module de-initialiser
445  *
446  *      Called while unloading a successfully installed watchdog module.
447  */
448
449 static void __exit watchdog_exit(void)
450 {
451         /* Stop the timer before we leave */
452         ali_stop();
453
454         /* Deregister */
455         unregister_reboot_notifier(&ali_notifier);
456         misc_deregister(&ali_miscdev);
457 }
458
459 module_init(watchdog_init);
460 module_exit(watchdog_exit);
461
462 MODULE_AUTHOR("Alan Cox");
463 MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
464 MODULE_LICENSE("GPL");
465 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);