blob: 90c091d9e0f56d715755a68ffc60eaec4f156b0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALi M7101 PMU Computer Watchdog Timer driver
3 *
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>
7 *
8 * (c)2002 Steve Hill <steve@navaho.co.uk>
9 *
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.
15 *
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>
20 */
21
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
35#include <asm/io.h>
36#include <asm/uaccess.h>
37#include <asm/system.h>
38
39#define OUR_NAME "alim7101_wdt"
40#define PFX OUR_NAME ": "
41
42#define WDT_ENABLE 0x9C
43#define WDT_DISABLE 0x8C
44
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
49
50/*
51 * We're going to use a 1 second timeout.
52 * If we reset the watchdog every ~250ms we should be safe. */
53
54#define WDT_INTERVAL (HZ/4+1)
55
56/*
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.
60 */
61
62#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
63static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
64module_param(timeout, int, 0);
65MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
66
67static int use_gpio = 0; /* Use the pic (for a1d revision alim7101) */
68module_param(use_gpio, int, 0);
69MODULE_PARM_DESC(use_gpio, "Use the gpio watchdog. (required by old cobalt boards)");
70
71static void wdt_timer_ping(unsigned long);
72static struct timer_list timer;
73static unsigned long next_heartbeat;
74static unsigned long wdt_is_open;
75static char wdt_expect_close;
76static struct pci_dev *alim7101_pmu;
77
78#ifdef CONFIG_WATCHDOG_NOWAYOUT
79static int nowayout = 1;
80#else
81static int nowayout = 0;
82#endif
83
84module_param(nowayout, int, 0);
85MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
86
87/*
88 * Whack the dog
89 */
90
91static void wdt_timer_ping(unsigned long data)
92{
93 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
94 * we agree to ping the WDT
95 */
96 char tmp;
97
98 if(time_before(jiffies, next_heartbeat))
99 {
100 /* Ping the WDT (this is actually a disarm/arm sequence) */
101 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
102 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
103 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
104 if (use_gpio) {
105 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
106 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp
107 | 0x20);
108 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp
109 & ~0x20);
110 }
111 } else {
112 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
113 }
114 /* Re-set the timer interval */
115 timer.expires = jiffies + WDT_INTERVAL;
116 add_timer(&timer);
117}
118
119/*
120 * Utility routines
121 */
122
123static void wdt_change(int writeval)
124{
125 char tmp;
126
127 pci_read_config_byte(alim7101_pmu, ALI_7101_WDT, &tmp);
128 if (writeval == WDT_ENABLE) {
129 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
130 if (use_gpio) {
131 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
132 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp & ~0x20);
133 }
134
135 } else {
136 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
137 if (use_gpio) {
138 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
139 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp | 0x20);
140 }
141 }
142}
143
144static void wdt_startup(void)
145{
146 next_heartbeat = jiffies + (timeout * HZ);
147
148 /* We must enable before we kick off the timer in case the timer
149 occurs as we ping it */
150
151 wdt_change(WDT_ENABLE);
152
153 /* Start the timer */
154 timer.expires = jiffies + WDT_INTERVAL;
155 add_timer(&timer);
156
157
158 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
159}
160
161static void wdt_turnoff(void)
162{
163 /* Stop the timer */
164 del_timer_sync(&timer);
165 wdt_change(WDT_DISABLE);
166 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
167}
168
169static void wdt_keepalive(void)
170{
171 /* user land ping */
172 next_heartbeat = jiffies + (timeout * HZ);
173}
174
175/*
176 * /dev/watchdog handling
177 */
178
179static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
180{
181 /* See if we got the magic character 'V' and reload the timer */
182 if(count) {
183 if (!nowayout) {
184 size_t ofs;
185
186 /* note: just in case someone wrote the magic character
187 * five months ago... */
188 wdt_expect_close = 0;
189
190 /* now scan */
191 for (ofs = 0; ofs != count; ofs++) {
192 char c;
193 if (get_user(c, buf+ofs))
194 return -EFAULT;
195 if (c == 'V')
196 wdt_expect_close = 42;
197 }
198 }
199 /* someone wrote to us, we should restart timer */
200 wdt_keepalive();
201 }
202 return count;
203}
204
205static int fop_open(struct inode * inode, struct file * file)
206{
207 /* Just in case we're already talking to someone... */
208 if(test_and_set_bit(0, &wdt_is_open))
209 return -EBUSY;
210 /* Good, fire up the show */
211 wdt_startup();
212 return nonseekable_open(inode, file);
213}
214
215static int fop_close(struct inode * inode, struct file * file)
216{
217 if(wdt_expect_close == 42)
218 wdt_turnoff();
219 else {
220 /* wim: shouldn't there be a: del_timer(&timer); */
221 printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
222 }
223 clear_bit(0, &wdt_is_open);
224 wdt_expect_close = 0;
225 return 0;
226}
227
228static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
229{
230 void __user *argp = (void __user *)arg;
231 int __user *p = argp;
232 static struct watchdog_info ident =
233 {
234 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
235 .firmware_version = 1,
236 .identity = "ALiM7101",
237 };
238
239 switch(cmd)
240 {
241 case WDIOC_GETSUPPORT:
242 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
243 case WDIOC_GETSTATUS:
244 case WDIOC_GETBOOTSTATUS:
245 return put_user(0, p);
246 case WDIOC_KEEPALIVE:
247 wdt_keepalive();
248 return 0;
249 case WDIOC_SETOPTIONS:
250 {
251 int new_options, retval = -EINVAL;
252
253 if(get_user(new_options, p))
254 return -EFAULT;
255
256 if(new_options & WDIOS_DISABLECARD) {
257 wdt_turnoff();
258 retval = 0;
259 }
260
261 if(new_options & WDIOS_ENABLECARD) {
262 wdt_startup();
263 retval = 0;
264 }
265
266 return retval;
267 }
268 case WDIOC_SETTIMEOUT:
269 {
270 int new_timeout;
271
272 if(get_user(new_timeout, p))
273 return -EFAULT;
274
275 if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
276 return -EINVAL;
277
278 timeout = new_timeout;
279 wdt_keepalive();
280 /* Fall through */
281 }
282 case WDIOC_GETTIMEOUT:
283 return put_user(timeout, p);
284 default:
285 return -ENOIOCTLCMD;
286 }
287}
288
289static struct file_operations wdt_fops = {
290 .owner= THIS_MODULE,
291 .llseek= no_llseek,
292 .write= fop_write,
293 .open= fop_open,
294 .release= fop_close,
295 .ioctl= fop_ioctl,
296};
297
298static struct miscdevice wdt_miscdev = {
299 .minor=WATCHDOG_MINOR,
300 .name="watchdog",
301 .fops=&wdt_fops,
302};
303
304/*
305 * Notifier for system down
306 */
307
308static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
309{
310 if (code==SYS_DOWN || code==SYS_HALT)
311 wdt_turnoff();
312
313 if (code==SYS_RESTART) {
314 /*
315 * Cobalt devices have no way of rebooting themselves other than
316 * getting the watchdog to pull reset, so we restart the watchdog on
317 * reboot with no heartbeat
318 */
319 wdt_change(WDT_ENABLE);
320 printk(KERN_INFO PFX "Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second.\n");
321 }
322 return NOTIFY_DONE;
323}
324
325/*
326 * The WDT needs to learn about soft shutdowns in order to
327 * turn the timebomb registers off.
328 */
329
330static struct notifier_block wdt_notifier=
331{
332 .notifier_call = wdt_notify_sys,
333};
334
335static void __exit alim7101_wdt_unload(void)
336{
337 wdt_turnoff();
338 /* Deregister */
339 misc_deregister(&wdt_miscdev);
340 unregister_reboot_notifier(&wdt_notifier);
341}
342
343static int __init alim7101_wdt_init(void)
344{
345 int rc = -EBUSY;
346 struct pci_dev *ali1543_south;
347 char tmp;
348
349 printk(KERN_INFO PFX "Steve Hill <steve@navaho.co.uk>.\n");
350 alim7101_pmu = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,NULL);
351 if (!alim7101_pmu) {
352 printk(KERN_INFO PFX "ALi M7101 PMU not present - WDT not set\n");
353 return -EBUSY;
354 }
355
356 /* Set the WDT in the PMU to 1 second */
357 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
358
359 ali1543_south = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
360 if (!ali1543_south) {
361 printk(KERN_INFO PFX "ALi 1543 South-Bridge not present - WDT not set\n");
362 return -EBUSY;
363 }
364 pci_read_config_byte(ali1543_south, 0x5e, &tmp);
365 if ((tmp & 0x1e) == 0x00) {
366 if (!use_gpio) {
367 printk(KERN_INFO PFX "Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
368 return -EBUSY;
369 }
370 nowayout = 1;
371 } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
372 printk(KERN_INFO PFX "ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
373 return -EBUSY;
374 }
375
376 if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
377 {
378 timeout = WATCHDOG_TIMEOUT;
379 printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
380 timeout);
381 }
382
383 init_timer(&timer);
384 timer.function = wdt_timer_ping;
385 timer.data = 1;
386
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;
392 }
393
394 rc = register_reboot_notifier(&wdt_notifier);
395 if (rc) {
396 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
397 rc);
398 goto err_out_miscdev;
399 }
400
401 if (nowayout) {
402 __module_get(THIS_MODULE);
403 }
404
405 printk(KERN_INFO PFX "WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
406 timeout, nowayout);
407 return 0;
408
409err_out_miscdev:
410 misc_deregister(&wdt_miscdev);
411err_out:
412 return rc;
413}
414
415module_init(alim7101_wdt_init);
416module_exit(alim7101_wdt_unload);
417
418MODULE_AUTHOR("Steve Hill");
419MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
420MODULE_LICENSE("GPL");
421MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);