blob: 88760cb5ec131aa65275db6f3abff46c842b0b08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Alan Cox173d95b2008-05-19 14:04:57 +010022#include <linux/uaccess.h>
23#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define WATCHDOG_NAME "ALi_M1535"
26#define PFX WATCHDOG_NAME ": "
27#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
28
29/* internal variables */
30static unsigned long ali_is_open;
31static char ali_expect_release;
32static struct pci_dev *ali_pci;
Alan Cox173d95b2008-05-19 14:04:57 +010033static u32 ali_timeout_bits; /* stores the computed timeout */
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070034static DEFINE_SPINLOCK(ali_lock); /* Guards the hardware */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* module parameters */
37static int timeout = WATCHDOG_TIMEOUT;
38module_param(timeout, int, 0);
Alan Cox173d95b2008-05-19 14:04:57 +010039MODULE_PARM_DESC(timeout,
40 "Watchdog timeout in seconds. (0 < timeout < 18000, default="
41 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Andrey Panin4bfdf372005-07-27 11:43:58 -070043static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044module_param(nowayout, int, 0);
Alan Cox173d95b2008-05-19 14:04:57 +010045MODULE_PARM_DESC(nowayout,
46 "Watchdog cannot be stopped once started (default="
47 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * ali_start - start watchdog countdown
51 *
52 * Starts the timer running providing the timer has a counter
53 * configuration set.
54 */
55
56static void ali_start(void)
57{
58 u32 val;
59
60 spin_lock(&ali_lock);
61
62 pci_read_config_dword(ali_pci, 0xCC, &val);
63 val &= ~0x3F; /* Mask count */
64 val |= (1<<25) | ali_timeout_bits;
65 pci_write_config_dword(ali_pci, 0xCC, val);
66
67 spin_unlock(&ali_lock);
68}
69
70/*
71 * ali_stop - stop the timer countdown
72 *
73 * Stop the ALi watchdog countdown
74 */
75
76static void ali_stop(void)
77{
78 u32 val;
79
80 spin_lock(&ali_lock);
81
82 pci_read_config_dword(ali_pci, 0xCC, &val);
83 val &= ~0x3F; /* Mask count to zero (disabled) */
84 val &= ~(1<<25);/* and for safety mask the reset enable */
85 pci_write_config_dword(ali_pci, 0xCC, val);
86
87 spin_unlock(&ali_lock);
88}
89
90/*
91 * ali_keepalive - send a keepalive to the watchdog
92 *
93 * Send a keepalive to the timer (actually we restart the timer).
94 */
95
96static void ali_keepalive(void)
97{
98 ali_start();
99}
100
101/*
102 * ali_settimer - compute the timer reload value
103 * @t: time in seconds
104 *
105 * Computes the timeout values needed
106 */
107
108static int ali_settimer(int t)
109{
Alan Cox173d95b2008-05-19 14:04:57 +0100110 if (t < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EINVAL;
Alan Cox173d95b2008-05-19 14:04:57 +0100112 else if (t < 60)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 ali_timeout_bits = t|(1<<6);
Alan Cox173d95b2008-05-19 14:04:57 +0100114 else if (t < 3600)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ali_timeout_bits = (t/60)|(1<<7);
Alan Cox173d95b2008-05-19 14:04:57 +0100116 else if (t < 18000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ali_timeout_bits = (t/300)|(1<<6)|(1<<7);
Alan Cox173d95b2008-05-19 14:04:57 +0100118 else
119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
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
141static ssize_t ali_write(struct file *file, const char __user *data,
Alan Cox173d95b2008-05-19 14:04:57 +0100142 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 /* See if we got the magic character 'V' and reload the timer */
145 if (len) {
146 if (!nowayout) {
147 size_t i;
148
Alan Cox173d95b2008-05-19 14:04:57 +0100149 /* note: just in case someone wrote the
150 magic character five months ago... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 ali_expect_release = 0;
152
Alan Cox173d95b2008-05-19 14:04:57 +0100153 /* scan to see whether or not we got
154 the magic character */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 for (i = 0; i != len; i++) {
156 char c;
Alan Cox173d95b2008-05-19 14:04:57 +0100157 if (get_user(c, data+i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EFAULT;
159 if (c == 'V')
160 ali_expect_release = 42;
161 }
162 }
163
164 /* someone wrote to us, we should reload the timer */
165 ali_start();
166 }
167 return len;
168}
169
170/*
171 * ali_ioctl - handle watchdog ioctls
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * @file: VFS file pointer
173 * @cmd: ioctl number
174 * @arg: arguments to the ioctl
175 *
176 * Handle the watchdog ioctls supported by the ALi driver. Really
177 * we want an extension to enable irq ack monitoring and the like
178 */
179
Alan Cox173d95b2008-05-19 14:04:57 +0100180static long ali_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 void __user *argp = (void __user *)arg;
183 int __user *p = argp;
184 static struct watchdog_info ident = {
185 .options = WDIOF_KEEPALIVEPING |
186 WDIOF_SETTIMEOUT |
187 WDIOF_MAGICCLOSE,
188 .firmware_version = 0,
189 .identity = "ALi M1535 WatchDog Timer",
190 };
191
192 switch (cmd) {
Alan Cox173d95b2008-05-19 14:04:57 +0100193 case WDIOC_GETSUPPORT:
194 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Alan Cox173d95b2008-05-19 14:04:57 +0100196 case WDIOC_GETSTATUS:
197 case WDIOC_GETBOOTSTATUS:
198 return put_user(0, p);
199 case WDIOC_KEEPALIVE:
200 ali_keepalive();
201 return 0;
202 case WDIOC_SETOPTIONS:
203 {
204 int new_options, retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Alan Cox173d95b2008-05-19 14:04:57 +0100206 if (get_user(new_options, p))
207 return -EFAULT;
208 if (new_options & WDIOS_DISABLECARD) {
209 ali_stop();
210 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Alan Cox173d95b2008-05-19 14:04:57 +0100212 if (new_options & WDIOS_ENABLECARD) {
213 ali_start();
214 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Alan Cox173d95b2008-05-19 14:04:57 +0100216 return retval;
217 }
218 case WDIOC_SETTIMEOUT:
219 {
220 int new_timeout;
221 if (get_user(new_timeout, p))
222 return -EFAULT;
223 if (ali_settimer(new_timeout))
224 return -EINVAL;
225 ali_keepalive();
226 /* Fall */
227 }
228 case WDIOC_GETTIMEOUT:
229 return put_user(timeout, p);
230 default:
231 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233}
234
235/*
236 * ali_open - handle open of ali watchdog
237 * @inode: inode from VFS
238 * @file: file from VFS
239 *
240 * Open the ALi watchdog device. Ensure only one person opens it
241 * at a time. Also start the watchdog running.
242 */
243
244static int ali_open(struct inode *inode, struct file *file)
245{
246 /* /dev/watchdog can only be opened once */
247 if (test_and_set_bit(0, &ali_is_open))
248 return -EBUSY;
249
250 /* Activate */
251 ali_start();
252 return nonseekable_open(inode, file);
253}
254
255/*
256 * ali_release - close an ALi watchdog
257 * @inode: inode from VFS
258 * @file: file from VFS
259 *
260 * Close the ALi watchdog device. Actual shutdown of the timer
261 * only occurs if the magic sequence has been set.
262 */
263
264static int ali_release(struct inode *inode, struct file *file)
265{
266 /*
267 * Shut off the timer.
268 */
Alan Cox173d95b2008-05-19 14:04:57 +0100269 if (ali_expect_release == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 ali_stop();
Alan Cox173d95b2008-05-19 14:04:57 +0100271 else {
272 printk(KERN_CRIT PFX
273 "Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ali_keepalive();
275 }
276 clear_bit(0, &ali_is_open);
277 ali_expect_release = 0;
278 return 0;
279}
280
281/*
282 * ali_notify_sys - System down notifier
283 *
284 * Notifier for system down
285 */
286
287
Alan Cox173d95b2008-05-19 14:04:57 +0100288static int ali_notify_sys(struct notifier_block *this,
289 unsigned long code, void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Alan Cox173d95b2008-05-19 14:04:57 +0100291 if (code == SYS_DOWN || code == SYS_HALT)
292 ali_stop(); /* Turn the WDT off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return NOTIFY_DONE;
294}
295
296/*
297 * Data for PCI driver interface
298 *
299 * This data only exists for exporting the supported
300 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
301 * register a pci_driver, because someone else might one day
302 * want to register another driver on the same PCI id.
303 */
304
305static struct pci_device_id ali_pci_tbl[] = {
Andrey Borzenkov6e420b72007-08-16 19:32:19 +0000306 { PCI_VENDOR_ID_AL, 0x1533, PCI_ANY_ID, PCI_ANY_ID,},
Rolf Eike Beerb7343f02005-07-27 11:43:42 -0700307 { PCI_VENDOR_ID_AL, 0x1535, PCI_ANY_ID, PCI_ANY_ID,},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 { 0, },
309};
310MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
311
312/*
313 * ali_find_watchdog - find a 1535 and 7101
314 *
315 * Scans the PCI hardware for a 1535 series bridge and matching 7101
316 * watchdog device. This may be overtight but it is better to be safe
317 */
318
319static int __init ali_find_watchdog(void)
320{
321 struct pci_dev *pdev;
322 u32 wdog;
323
Andrey Borzenkov6e420b72007-08-16 19:32:19 +0000324 /* Check for a 1533/1535 series bridge */
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159325 pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1535, NULL);
Andrey Borzenkov6e420b72007-08-16 19:32:19 +0000326 if (pdev == NULL)
327 pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1533, NULL);
328 if (pdev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -ENODEV;
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159330 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Check for the a 7101 PMU */
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159333 pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x7101, NULL);
Alan Cox173d95b2008-05-19 14:04:57 +0100334 if (pdev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -ENODEV;
336
Alan Cox173d95b2008-05-19 14:04:57 +0100337 if (pci_enable_device(pdev)) {
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159338 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EIO;
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 ali_pci = pdev;
343
344 /*
345 * Initialize the timer bits
346 */
347 pci_read_config_dword(pdev, 0xCC, &wdog);
348
Alan Cox173d95b2008-05-19 14:04:57 +0100349 /* Timer bits */
350 wdog &= ~0x3F;
351 /* Issued events */
352 wdog &= ~((1<<27)|(1<<26)|(1<<25)|(1<<24));
353 /* No monitor bits */
354 wdog &= ~((1<<16)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 pci_write_config_dword(pdev, 0xCC, wdog);
357
358 return 0;
359}
360
361/*
362 * Kernel Interfaces
363 */
364
Arjan van de Ven62322d22006-07-03 00:24:21 -0700365static const struct file_operations ali_fops = {
Alan Cox173d95b2008-05-19 14:04:57 +0100366 .owner = THIS_MODULE,
367 .llseek = no_llseek,
368 .write = ali_write,
369 .unlocked_ioctl = ali_ioctl,
370 .open = ali_open,
371 .release = ali_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374static struct miscdevice ali_miscdev = {
375 .minor = WATCHDOG_MINOR,
376 .name = "watchdog",
377 .fops = &ali_fops,
378};
379
380static struct notifier_block ali_notifier = {
381 .notifier_call = ali_notify_sys,
382};
383
384/*
385 * watchdog_init - module initialiser
386 *
387 * Scan for a suitable watchdog and if so initialize it. Return an error
388 * if we cannot, the error causes the module to unload
389 */
390
391static int __init watchdog_init(void)
392{
393 int ret;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Check whether or not the hardware watchdog is there */
Alan Cox173d95b2008-05-19 14:04:57 +0100396 if (ali_find_watchdog() != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Alan Cox173d95b2008-05-19 14:04:57 +0100399 /* Check that the timeout value is within it's range;
400 if not reset to the default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (timeout < 1 || timeout >= 18000) {
402 timeout = WATCHDOG_TIMEOUT;
Alan Cox173d95b2008-05-19 14:04:57 +0100403 printk(KERN_INFO PFX
404 "timeout value must be 0 < timeout < 18000, using %d\n",
405 timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 /* Calculate the watchdog's timeout */
409 ali_settimer(timeout);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 ret = register_reboot_notifier(&ali_notifier);
412 if (ret != 0) {
Alan Cox173d95b2008-05-19 14:04:57 +0100413 printk(KERN_ERR PFX
414 "cannot register reboot notifier (err=%d)\n", ret);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000415 goto out;
416 }
417
418 ret = misc_register(&ali_miscdev);
419 if (ret != 0) {
Alan Cox173d95b2008-05-19 14:04:57 +0100420 printk(KERN_ERR PFX
421 "cannot register miscdev on minor=%d (err=%d)\n",
422 WATCHDOG_MINOR, ret);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000423 goto unreg_reboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
427 timeout, nowayout);
428
429out:
430 return ret;
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000431unreg_reboot:
432 unregister_reboot_notifier(&ali_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto out;
434}
435
436/*
437 * watchdog_exit - module de-initialiser
438 *
439 * Called while unloading a successfully installed watchdog module.
440 */
441
442static void __exit watchdog_exit(void)
443{
444 /* Stop the timer before we leave */
445 ali_stop();
446
447 /* Deregister */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 misc_deregister(&ali_miscdev);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000449 unregister_reboot_notifier(&ali_notifier);
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159450 pci_dev_put(ali_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453module_init(watchdog_init);
454module_exit(watchdog_exit);
455
456MODULE_AUTHOR("Alan Cox");
457MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
458MODULE_LICENSE("GPL");
459MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);