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.
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/types.h>
27 #include <linux/timer.h>
28 #include <linux/miscdevice.h>
29 #include <linux/watchdog.h>
30 #include <linux/ioport.h>
31 #include <linux/notifier.h>
32 #include <linux/reboot.h>
33 #include <linux/init.h>
35 #include <linux/pci.h>
37 #include <linux/uaccess.h>
39 #include <asm/system.h>
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
) ")");
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
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
));
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);
114 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
116 /* Re-set the timer interval */
117 mod_timer(&timer
, jiffies
+ WDT_INTERVAL
);
124 static void wdt_change(int writeval
)
128 pci_read_config_byte(alim7101_pmu
, ALI_7101_WDT
, &tmp
);
129 if (writeval
== WDT_ENABLE
) {
130 pci_write_config_byte(alim7101_pmu
,
131 ALI_7101_WDT
, (tmp
| ALI_WDT_ARM
));
133 pci_read_config_byte(alim7101_pmu
,
134 ALI_7101_GPIO_O
, &tmp
);
135 pci_write_config_byte(alim7101_pmu
,
136 ALI_7101_GPIO_O
, tmp
& ~0x20);
140 pci_write_config_byte(alim7101_pmu
,
141 ALI_7101_WDT
, (tmp
& ~ALI_WDT_ARM
));
143 pci_read_config_byte(alim7101_pmu
,
144 ALI_7101_GPIO_O
, &tmp
);
145 pci_write_config_byte(alim7101_pmu
,
146 ALI_7101_GPIO_O
, tmp
| 0x20);
151 static void wdt_startup(void)
153 next_heartbeat
= jiffies
+ (timeout
* HZ
);
155 /* We must enable before we kick off the timer in case the timer
156 occurs as we ping it */
158 wdt_change(WDT_ENABLE
);
160 /* Start the timer */
161 mod_timer(&timer
, jiffies
+ WDT_INTERVAL
);
163 pr_info("Watchdog timer is now enabled\n");
166 static void wdt_turnoff(void)
169 del_timer_sync(&timer
);
170 wdt_change(WDT_DISABLE
);
171 pr_info("Watchdog timer is now disabled...\n");
174 static void wdt_keepalive(void)
177 next_heartbeat
= jiffies
+ (timeout
* HZ
);
181 * /dev/watchdog handling
184 static ssize_t
fop_write(struct file
*file
, const char __user
*buf
,
185 size_t count
, loff_t
*ppos
)
187 /* See if we got the magic character 'V' and reload the timer */
192 /* note: just in case someone wrote the magic character
193 * five months ago... */
194 wdt_expect_close
= 0;
197 for (ofs
= 0; ofs
!= count
; ofs
++) {
199 if (get_user(c
, buf
+ ofs
))
202 wdt_expect_close
= 42;
205 /* someone wrote to us, we should restart timer */
211 static int fop_open(struct inode
*inode
, struct file
*file
)
213 /* Just in case we're already talking to someone... */
214 if (test_and_set_bit(0, &wdt_is_open
))
216 /* Good, fire up the show */
218 return nonseekable_open(inode
, file
);
221 static int fop_close(struct inode
*inode
, struct file
*file
)
223 if (wdt_expect_close
== 42)
226 /* wim: shouldn't there be a: del_timer(&timer); */
227 pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
229 clear_bit(0, &wdt_is_open
);
230 wdt_expect_close
= 0;
234 static long fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
236 void __user
*argp
= (void __user
*)arg
;
237 int __user
*p
= argp
;
238 static const struct watchdog_info ident
= {
239 .options
= WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
241 .firmware_version
= 1,
242 .identity
= "ALiM7101",
246 case WDIOC_GETSUPPORT
:
247 return copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
248 case WDIOC_GETSTATUS
:
249 case WDIOC_GETBOOTSTATUS
:
250 return put_user(0, p
);
251 case WDIOC_SETOPTIONS
:
253 int new_options
, retval
= -EINVAL
;
255 if (get_user(new_options
, p
))
257 if (new_options
& WDIOS_DISABLECARD
) {
261 if (new_options
& WDIOS_ENABLECARD
) {
267 case WDIOC_KEEPALIVE
:
270 case WDIOC_SETTIMEOUT
:
274 if (get_user(new_timeout
, p
))
276 /* arbitrary upper limit */
277 if (new_timeout
< 1 || new_timeout
> 3600)
279 timeout
= new_timeout
;
283 case WDIOC_GETTIMEOUT
:
284 return put_user(timeout
, p
);
290 static const struct file_operations wdt_fops
= {
291 .owner
= THIS_MODULE
,
295 .release
= fop_close
,
296 .unlocked_ioctl
= fop_ioctl
,
299 static struct miscdevice wdt_miscdev
= {
300 .minor
= WATCHDOG_MINOR
,
306 * Notifier for system down
309 static int wdt_notify_sys(struct notifier_block
*this,
310 unsigned long code
, void *unused
)
312 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
315 if (code
== SYS_RESTART
) {
317 * Cobalt devices have no way of rebooting themselves other
318 * than getting the watchdog to pull reset, so we restart the
319 * watchdog on reboot with no heartbeat
321 wdt_change(WDT_ENABLE
);
322 pr_info("Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second\n");
328 * The WDT needs to learn about soft shutdowns in order to
329 * turn the timebomb registers off.
332 static struct notifier_block wdt_notifier
= {
333 .notifier_call
= wdt_notify_sys
,
336 static void __exit
alim7101_wdt_unload(void)
340 misc_deregister(&wdt_miscdev
);
341 unregister_reboot_notifier(&wdt_notifier
);
342 pci_dev_put(alim7101_pmu
);
345 static int __init
alim7101_wdt_init(void)
348 struct pci_dev
*ali1543_south
;
351 pr_info("Steve Hill <steve@navaho.co.uk>\n");
352 alim7101_pmu
= pci_get_device(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M7101
,
355 pr_info("ALi M7101 PMU not present - WDT not set\n");
359 /* Set the WDT in the PMU to 1 second */
360 pci_write_config_byte(alim7101_pmu
, ALI_7101_WDT
, 0x02);
362 ali1543_south
= pci_get_device(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M1533
,
364 if (!ali1543_south
) {
365 pr_info("ALi 1543 South-Bridge not present - WDT not set\n");
368 pci_read_config_byte(ali1543_south
, 0x5e, &tmp
);
369 pci_dev_put(ali1543_south
);
370 if ((tmp
& 0x1e) == 0x00) {
372 pr_info("Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
376 } else if ((tmp
& 0x1e) != 0x12 && (tmp
& 0x1e) != 0x00) {
377 pr_info("ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
381 if (timeout
< 1 || timeout
> 3600) {
382 /* arbitrary upper limit */
383 timeout
= WATCHDOG_TIMEOUT
;
384 pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
388 rc
= register_reboot_notifier(&wdt_notifier
);
390 pr_err("cannot register reboot notifier (err=%d)\n", rc
);
394 rc
= misc_register(&wdt_miscdev
);
396 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
397 wdt_miscdev
.minor
, rc
);
402 __module_get(THIS_MODULE
);
404 pr_info("WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
409 unregister_reboot_notifier(&wdt_notifier
);
411 pci_dev_put(alim7101_pmu
);
415 module_init(alim7101_wdt_init
);
416 module_exit(alim7101_wdt_unload
);
418 static DEFINE_PCI_DEVICE_TABLE(alim7101_pci_tbl
) __used
= {
419 { PCI_DEVICE(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M1533
) },
420 { PCI_DEVICE(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M7101
) },
424 MODULE_DEVICE_TABLE(pci
, alim7101_pci_tbl
);
426 MODULE_AUTHOR("Steve Hill");
427 MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
428 MODULE_LICENSE("GPL");
429 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);