Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / watchdog / alim7101_wdt.c
blob238273c98656b82d3464c17dd3c6e29a8c95e788
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>
35 #include <asm/io.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
39 #define OUR_NAME "alim7101_wdt"
40 #define PFX OUR_NAME ": "
42 #define WDT_ENABLE 0x9C
43 #define WDT_DISABLE 0x8C
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
51 * We're going to use a 1 second timeout.
52 * If we reset the watchdog every ~250ms we should be safe. */
54 #define WDT_INTERVAL (HZ/4+1)
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.
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) ")");
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)");
71 static void wdt_timer_ping(unsigned long);
72 static DEFINE_TIMER(timer, wdt_timer_ping, 0, 1);
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;
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 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
84 * Whack the dog
87 static void wdt_timer_ping(unsigned long data)
89 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
90 * we agree to ping the WDT
92 char tmp;
94 if(time_before(jiffies, next_heartbeat))
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);
107 } else {
108 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
110 /* Re-set the timer interval */
111 mod_timer(&timer, jiffies + WDT_INTERVAL);
115 * Utility routines
118 static void wdt_change(int writeval)
120 char tmp;
122 pci_read_config_byte(alim7101_pmu, ALI_7101_WDT, &tmp);
123 if (writeval == WDT_ENABLE) {
124 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
125 if (use_gpio) {
126 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
127 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp & ~0x20);
130 } else {
131 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
132 if (use_gpio) {
133 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
134 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp | 0x20);
139 static void wdt_startup(void)
141 next_heartbeat = jiffies + (timeout * HZ);
143 /* We must enable before we kick off the timer in case the timer
144 occurs as we ping it */
146 wdt_change(WDT_ENABLE);
148 /* Start the timer */
149 mod_timer(&timer, jiffies + WDT_INTERVAL);
151 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
154 static void wdt_turnoff(void)
156 /* Stop the timer */
157 del_timer_sync(&timer);
158 wdt_change(WDT_DISABLE);
159 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
162 static void wdt_keepalive(void)
164 /* user land ping */
165 next_heartbeat = jiffies + (timeout * HZ);
169 * /dev/watchdog handling
172 static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
174 /* See if we got the magic character 'V' and reload the timer */
175 if(count) {
176 if (!nowayout) {
177 size_t ofs;
179 /* note: just in case someone wrote the magic character
180 * five months ago... */
181 wdt_expect_close = 0;
183 /* now scan */
184 for (ofs = 0; ofs != count; ofs++) {
185 char c;
186 if (get_user(c, buf+ofs))
187 return -EFAULT;
188 if (c == 'V')
189 wdt_expect_close = 42;
192 /* someone wrote to us, we should restart timer */
193 wdt_keepalive();
195 return count;
198 static int fop_open(struct inode * inode, struct file * file)
200 /* Just in case we're already talking to someone... */
201 if(test_and_set_bit(0, &wdt_is_open))
202 return -EBUSY;
203 /* Good, fire up the show */
204 wdt_startup();
205 return nonseekable_open(inode, file);
208 static int fop_close(struct inode * inode, struct file * file)
210 if(wdt_expect_close == 42)
211 wdt_turnoff();
212 else {
213 /* wim: shouldn't there be a: del_timer(&timer); */
214 printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
216 clear_bit(0, &wdt_is_open);
217 wdt_expect_close = 0;
218 return 0;
221 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
223 void __user *argp = (void __user *)arg;
224 int __user *p = argp;
225 static struct watchdog_info ident =
227 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
228 .firmware_version = 1,
229 .identity = "ALiM7101",
232 switch(cmd)
234 case WDIOC_GETSUPPORT:
235 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
236 case WDIOC_GETSTATUS:
237 case WDIOC_GETBOOTSTATUS:
238 return put_user(0, p);
239 case WDIOC_KEEPALIVE:
240 wdt_keepalive();
241 return 0;
242 case WDIOC_SETOPTIONS:
244 int new_options, retval = -EINVAL;
246 if(get_user(new_options, p))
247 return -EFAULT;
249 if(new_options & WDIOS_DISABLECARD) {
250 wdt_turnoff();
251 retval = 0;
254 if(new_options & WDIOS_ENABLECARD) {
255 wdt_startup();
256 retval = 0;
259 return retval;
261 case WDIOC_SETTIMEOUT:
263 int new_timeout;
265 if(get_user(new_timeout, p))
266 return -EFAULT;
268 if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
269 return -EINVAL;
271 timeout = new_timeout;
272 wdt_keepalive();
273 /* Fall through */
275 case WDIOC_GETTIMEOUT:
276 return put_user(timeout, p);
277 default:
278 return -ENOTTY;
282 static const struct file_operations wdt_fops = {
283 .owner= THIS_MODULE,
284 .llseek= no_llseek,
285 .write= fop_write,
286 .open= fop_open,
287 .release= fop_close,
288 .ioctl= fop_ioctl,
291 static struct miscdevice wdt_miscdev = {
292 .minor=WATCHDOG_MINOR,
293 .name="watchdog",
294 .fops=&wdt_fops,
298 * Notifier for system down
301 static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
303 if (code==SYS_DOWN || code==SYS_HALT)
304 wdt_turnoff();
306 if (code==SYS_RESTART) {
308 * Cobalt devices have no way of rebooting themselves other than
309 * getting the watchdog to pull reset, so we restart the watchdog on
310 * reboot with no heartbeat
312 wdt_change(WDT_ENABLE);
313 printk(KERN_INFO PFX "Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second.\n");
315 return NOTIFY_DONE;
319 * The WDT needs to learn about soft shutdowns in order to
320 * turn the timebomb registers off.
323 static struct notifier_block wdt_notifier=
325 .notifier_call = wdt_notify_sys,
328 static void __exit alim7101_wdt_unload(void)
330 wdt_turnoff();
331 /* Deregister */
332 misc_deregister(&wdt_miscdev);
333 unregister_reboot_notifier(&wdt_notifier);
334 pci_dev_put(alim7101_pmu);
337 static int __init alim7101_wdt_init(void)
339 int rc = -EBUSY;
340 struct pci_dev *ali1543_south;
341 char tmp;
343 printk(KERN_INFO PFX "Steve Hill <steve@navaho.co.uk>.\n");
344 alim7101_pmu = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
345 NULL);
346 if (!alim7101_pmu) {
347 printk(KERN_INFO PFX "ALi M7101 PMU not present - WDT not set\n");
348 return -EBUSY;
351 /* Set the WDT in the PMU to 1 second */
352 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
354 ali1543_south = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
355 NULL);
356 if (!ali1543_south) {
357 printk(KERN_INFO PFX "ALi 1543 South-Bridge not present - WDT not set\n");
358 goto err_out;
360 pci_read_config_byte(ali1543_south, 0x5e, &tmp);
361 pci_dev_put(ali1543_south);
362 if ((tmp & 0x1e) == 0x00) {
363 if (!use_gpio) {
364 printk(KERN_INFO PFX "Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
365 goto err_out;
367 nowayout = 1;
368 } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
369 printk(KERN_INFO PFX "ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
370 goto err_out;
373 if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
375 timeout = WATCHDOG_TIMEOUT;
376 printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
377 timeout);
380 rc = register_reboot_notifier(&wdt_notifier);
381 if (rc) {
382 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
383 rc);
384 goto err_out;
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_reboot;
394 if (nowayout) {
395 __module_get(THIS_MODULE);
398 printk(KERN_INFO PFX "WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
399 timeout, nowayout);
400 return 0;
402 err_out_reboot:
403 unregister_reboot_notifier(&wdt_notifier);
404 err_out:
405 pci_dev_put(alim7101_pmu);
406 return rc;
409 module_init(alim7101_wdt_init);
410 module_exit(alim7101_wdt_unload);
412 static struct pci_device_id alim7101_pci_tbl[] __devinitdata = {
413 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533) },
414 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
418 MODULE_DEVICE_TABLE(pci, alim7101_pci_tbl);
420 MODULE_AUTHOR("Steve Hill");
421 MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
422 MODULE_LICENSE("GPL");
423 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);