Remove all #inclusions of asm/system.h
[linux-2.6.git] / drivers / watchdog / alim7101_wdt.c
blob486841895e9f04c2540d229c01bec01dea422360
1 /*
2 * ALi M7101 PMU Computer Watchdog Timer driver
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>
8 * (c)2002 Steve Hill <steve@navaho.co.uk>
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.
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>
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 #include <linux/io.h>
35 #include <linux/uaccess.h>
38 #define OUR_NAME "alim7101_wdt"
39 #define PFX OUR_NAME ": "
41 #define WDT_ENABLE 0x9C
42 #define WDT_DISABLE 0x8C
44 #define ALI_7101_WDT 0x92
45 #define ALI_7101_GPIO 0x7D
46 #define ALI_7101_GPIO_O 0x7E
47 #define ALI_WDT_ARM 0x01
50 * We're going to use a 1 second timeout.
51 * If we reset the watchdog every ~250ms we should be safe. */
53 #define WDT_INTERVAL (HZ/4+1)
56 * We must not require too good response from the userspace daemon.
57 * Here we require the userspace daemon to send us a heartbeat
58 * char to /dev/watchdog every 30 seconds.
61 #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
62 /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
63 static int timeout = WATCHDOG_TIMEOUT;
64 module_param(timeout, int, 0);
65 MODULE_PARM_DESC(timeout,
66 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
67 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
69 static int use_gpio; /* Use the pic (for a1d revision alim7101) */
70 module_param(use_gpio, int, 0);
71 MODULE_PARM_DESC(use_gpio,
72 "Use the gpio watchdog (required by old cobalt boards).");
74 static void wdt_timer_ping(unsigned long);
75 static DEFINE_TIMER(timer, wdt_timer_ping, 0, 1);
76 static unsigned long next_heartbeat;
77 static unsigned long wdt_is_open;
78 static char wdt_expect_close;
79 static struct pci_dev *alim7101_pmu;
81 static int nowayout = WATCHDOG_NOWAYOUT;
82 module_param(nowayout, int, 0);
83 MODULE_PARM_DESC(nowayout,
84 "Watchdog cannot be stopped once started (default="
85 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
88 * Whack the dog
91 static void wdt_timer_ping(unsigned long data)
93 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
94 * we agree to ping the WDT
96 char tmp;
98 if (time_before(jiffies, next_heartbeat)) {
99 /* Ping the WDT (this is actually a disarm/arm sequence) */
100 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
101 pci_write_config_byte(alim7101_pmu,
102 ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
103 pci_write_config_byte(alim7101_pmu,
104 ALI_7101_WDT, (tmp | ALI_WDT_ARM));
105 if (use_gpio) {
106 pci_read_config_byte(alim7101_pmu,
107 ALI_7101_GPIO_O, &tmp);
108 pci_write_config_byte(alim7101_pmu,
109 ALI_7101_GPIO_O, tmp | 0x20);
110 pci_write_config_byte(alim7101_pmu,
111 ALI_7101_GPIO_O, tmp & ~0x20);
113 } else {
114 printk(KERN_WARNING PFX
115 "Heartbeat lost! Will not ping the watchdog\n");
117 /* Re-set the timer interval */
118 mod_timer(&timer, jiffies + WDT_INTERVAL);
122 * Utility routines
125 static void wdt_change(int writeval)
127 char tmp;
129 pci_read_config_byte(alim7101_pmu, ALI_7101_WDT, &tmp);
130 if (writeval == WDT_ENABLE) {
131 pci_write_config_byte(alim7101_pmu,
132 ALI_7101_WDT, (tmp | ALI_WDT_ARM));
133 if (use_gpio) {
134 pci_read_config_byte(alim7101_pmu,
135 ALI_7101_GPIO_O, &tmp);
136 pci_write_config_byte(alim7101_pmu,
137 ALI_7101_GPIO_O, tmp & ~0x20);
140 } else {
141 pci_write_config_byte(alim7101_pmu,
142 ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
143 if (use_gpio) {
144 pci_read_config_byte(alim7101_pmu,
145 ALI_7101_GPIO_O, &tmp);
146 pci_write_config_byte(alim7101_pmu,
147 ALI_7101_GPIO_O, tmp | 0x20);
152 static void wdt_startup(void)
154 next_heartbeat = jiffies + (timeout * HZ);
156 /* We must enable before we kick off the timer in case the timer
157 occurs as we ping it */
159 wdt_change(WDT_ENABLE);
161 /* Start the timer */
162 mod_timer(&timer, jiffies + WDT_INTERVAL);
164 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
167 static void wdt_turnoff(void)
169 /* Stop the timer */
170 del_timer_sync(&timer);
171 wdt_change(WDT_DISABLE);
172 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
175 static void wdt_keepalive(void)
177 /* user land ping */
178 next_heartbeat = jiffies + (timeout * HZ);
182 * /dev/watchdog handling
185 static ssize_t fop_write(struct file *file, const char __user *buf,
186 size_t count, loff_t *ppos)
188 /* See if we got the magic character 'V' and reload the timer */
189 if (count) {
190 if (!nowayout) {
191 size_t ofs;
193 /* note: just in case someone wrote the magic character
194 * five months ago... */
195 wdt_expect_close = 0;
197 /* now scan */
198 for (ofs = 0; ofs != count; ofs++) {
199 char c;
200 if (get_user(c, buf + ofs))
201 return -EFAULT;
202 if (c == 'V')
203 wdt_expect_close = 42;
206 /* someone wrote to us, we should restart timer */
207 wdt_keepalive();
209 return count;
212 static int fop_open(struct inode *inode, struct file *file)
214 /* Just in case we're already talking to someone... */
215 if (test_and_set_bit(0, &wdt_is_open))
216 return -EBUSY;
217 /* Good, fire up the show */
218 wdt_startup();
219 return nonseekable_open(inode, file);
222 static int fop_close(struct inode *inode, struct file *file)
224 if (wdt_expect_close == 42)
225 wdt_turnoff();
226 else {
227 /* wim: shouldn't there be a: del_timer(&timer); */
228 printk(KERN_CRIT PFX
229 "device file closed unexpectedly. Will not stop the WDT!\n");
231 clear_bit(0, &wdt_is_open);
232 wdt_expect_close = 0;
233 return 0;
236 static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
238 void __user *argp = (void __user *)arg;
239 int __user *p = argp;
240 static const struct watchdog_info ident = {
241 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
242 | WDIOF_MAGICCLOSE,
243 .firmware_version = 1,
244 .identity = "ALiM7101",
247 switch (cmd) {
248 case WDIOC_GETSUPPORT:
249 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
250 case WDIOC_GETSTATUS:
251 case WDIOC_GETBOOTSTATUS:
252 return put_user(0, p);
253 case WDIOC_SETOPTIONS:
255 int new_options, retval = -EINVAL;
257 if (get_user(new_options, p))
258 return -EFAULT;
259 if (new_options & WDIOS_DISABLECARD) {
260 wdt_turnoff();
261 retval = 0;
263 if (new_options & WDIOS_ENABLECARD) {
264 wdt_startup();
265 retval = 0;
267 return retval;
269 case WDIOC_KEEPALIVE:
270 wdt_keepalive();
271 return 0;
272 case WDIOC_SETTIMEOUT:
274 int new_timeout;
276 if (get_user(new_timeout, p))
277 return -EFAULT;
278 /* arbitrary upper limit */
279 if (new_timeout < 1 || new_timeout > 3600)
280 return -EINVAL;
281 timeout = new_timeout;
282 wdt_keepalive();
283 /* Fall through */
285 case WDIOC_GETTIMEOUT:
286 return put_user(timeout, p);
287 default:
288 return -ENOTTY;
292 static const struct file_operations wdt_fops = {
293 .owner = THIS_MODULE,
294 .llseek = no_llseek,
295 .write = fop_write,
296 .open = fop_open,
297 .release = fop_close,
298 .unlocked_ioctl = fop_ioctl,
301 static struct miscdevice wdt_miscdev = {
302 .minor = WATCHDOG_MINOR,
303 .name = "watchdog",
304 .fops = &wdt_fops,
308 * Notifier for system down
311 static int wdt_notify_sys(struct notifier_block *this,
312 unsigned long code, void *unused)
314 if (code == SYS_DOWN || code == SYS_HALT)
315 wdt_turnoff();
317 if (code == SYS_RESTART) {
319 * Cobalt devices have no way of rebooting themselves other
320 * than getting the watchdog to pull reset, so we restart the
321 * watchdog on reboot with no heartbeat
323 wdt_change(WDT_ENABLE);
324 printk(KERN_INFO PFX "Watchdog timer is now enabled "
325 "with no heartbeat - should reboot in ~1 second.\n");
327 return NOTIFY_DONE;
331 * The WDT needs to learn about soft shutdowns in order to
332 * turn the timebomb registers off.
335 static struct notifier_block wdt_notifier = {
336 .notifier_call = wdt_notify_sys,
339 static void __exit alim7101_wdt_unload(void)
341 wdt_turnoff();
342 /* Deregister */
343 misc_deregister(&wdt_miscdev);
344 unregister_reboot_notifier(&wdt_notifier);
345 pci_dev_put(alim7101_pmu);
348 static int __init alim7101_wdt_init(void)
350 int rc = -EBUSY;
351 struct pci_dev *ali1543_south;
352 char tmp;
354 printk(KERN_INFO PFX "Steve Hill <steve@navaho.co.uk>.\n");
355 alim7101_pmu = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
356 NULL);
357 if (!alim7101_pmu) {
358 printk(KERN_INFO PFX
359 "ALi M7101 PMU not present - WDT not set\n");
360 return -EBUSY;
363 /* Set the WDT in the PMU to 1 second */
364 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
366 ali1543_south = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
367 NULL);
368 if (!ali1543_south) {
369 printk(KERN_INFO PFX
370 "ALi 1543 South-Bridge not present - WDT not set\n");
371 goto err_out;
373 pci_read_config_byte(ali1543_south, 0x5e, &tmp);
374 pci_dev_put(ali1543_south);
375 if ((tmp & 0x1e) == 0x00) {
376 if (!use_gpio) {
377 printk(KERN_INFO PFX
378 "Detected old alim7101 revision 'a1d'. "
379 "If this is a cobalt board, set the 'use_gpio' "
380 "module parameter.\n");
381 goto err_out;
383 nowayout = 1;
384 } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
385 printk(KERN_INFO PFX
386 "ALi 1543 South-Bridge does not have the correct "
387 "revision number (???1001?) - WDT not set\n");
388 goto err_out;
391 if (timeout < 1 || timeout > 3600) {
392 /* arbitrary upper limit */
393 timeout = WATCHDOG_TIMEOUT;
394 printk(KERN_INFO PFX
395 "timeout value must be 1 <= x <= 3600, using %d\n",
396 timeout);
399 rc = register_reboot_notifier(&wdt_notifier);
400 if (rc) {
401 printk(KERN_ERR PFX
402 "cannot register reboot notifier (err=%d)\n", rc);
403 goto err_out;
406 rc = misc_register(&wdt_miscdev);
407 if (rc) {
408 printk(KERN_ERR PFX
409 "cannot register miscdev on minor=%d (err=%d)\n",
410 wdt_miscdev.minor, rc);
411 goto err_out_reboot;
414 if (nowayout)
415 __module_get(THIS_MODULE);
417 printk(KERN_INFO PFX "WDT driver for ALi M7101 initialised. "
418 "timeout=%d sec (nowayout=%d)\n",
419 timeout, nowayout);
420 return 0;
422 err_out_reboot:
423 unregister_reboot_notifier(&wdt_notifier);
424 err_out:
425 pci_dev_put(alim7101_pmu);
426 return rc;
429 module_init(alim7101_wdt_init);
430 module_exit(alim7101_wdt_unload);
432 static DEFINE_PCI_DEVICE_TABLE(alim7101_pci_tbl) __used = {
433 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533) },
434 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
438 MODULE_DEVICE_TABLE(pci, alim7101_pci_tbl);
440 MODULE_AUTHOR("Steve Hill");
441 MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
442 MODULE_LICENSE("GPL");
443 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);