blob: b9e9558ab218c56228e27355dc4535add1e14f17 [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
Joe Perches27c766a2012-02-15 15:06:19 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
34#include <linux/fs.h>
35#include <linux/pci.h>
Alan Cox173d95b2008-05-19 14:04:57 +010036#include <linux/io.h>
37#include <linux/uaccess.h>
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/system.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define WDT_ENABLE 0x9C
42#define WDT_DISABLE 0x8C
43
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
48
49/*
50 * We're going to use a 1 second timeout.
51 * If we reset the watchdog every ~250ms we should be safe. */
52
53#define WDT_INTERVAL (HZ/4+1)
54
55/*
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.
59 */
60
61#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
Alan Cox173d95b2008-05-19 14:04:57 +010062/* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
63static int timeout = WATCHDOG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064module_param(timeout, int, 0);
Alan Cox173d95b2008-05-19 14:04:57 +010065MODULE_PARM_DESC(timeout,
66 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
67 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Alan Cox173d95b2008-05-19 14:04:57 +010069static int use_gpio; /* Use the pic (for a1d revision alim7101) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070module_param(use_gpio, int, 0);
Alan Cox173d95b2008-05-19 14:04:57 +010071MODULE_PARM_DESC(use_gpio,
72 "Use the gpio watchdog (required by old cobalt boards).");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static void wdt_timer_ping(unsigned long);
Jiri Slaby82eb7c52007-02-08 18:39:36 +010075static DEFINE_TIMER(timer, wdt_timer_ping, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static unsigned long next_heartbeat;
77static unsigned long wdt_is_open;
78static char wdt_expect_close;
79static struct pci_dev *alim7101_pmu;
80
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010081static bool nowayout = WATCHDOG_NOWAYOUT;
82module_param(nowayout, bool, 0);
Alan Cox173d95b2008-05-19 14:04:57 +010083MODULE_PARM_DESC(nowayout,
84 "Watchdog cannot be stopped once started (default="
85 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
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 */
Alan Cox173d95b2008-05-19 14:04:57 +010096 char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Alan Cox173d95b2008-05-19 14:04:57 +010098 if (time_before(jiffies, next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /* Ping the WDT (this is actually a disarm/arm sequence) */
100 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
Alan Cox173d95b2008-05-19 14:04:57 +0100101 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (use_gpio) {
Alan Cox173d95b2008-05-19 14:04:57 +0100106 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800114 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116 /* Re-set the timer interval */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100117 mod_timer(&timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/*
121 * Utility routines
122 */
123
124static void wdt_change(int writeval)
125{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000126 char tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 pci_read_config_byte(alim7101_pmu, ALI_7101_WDT, &tmp);
129 if (writeval == WDT_ENABLE) {
Alan Cox173d95b2008-05-19 14:04:57 +0100130 pci_write_config_byte(alim7101_pmu,
131 ALI_7101_WDT, (tmp | ALI_WDT_ARM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (use_gpio) {
Alan Cox173d95b2008-05-19 14:04:57 +0100133 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
139 } else {
Alan Cox173d95b2008-05-19 14:04:57 +0100140 pci_write_config_byte(alim7101_pmu,
141 ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (use_gpio) {
Alan Cox173d95b2008-05-19 14:04:57 +0100143 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 }
149}
150
151static void wdt_startup(void)
152{
153 next_heartbeat = jiffies + (timeout * HZ);
154
155 /* We must enable before we kick off the timer in case the timer
156 occurs as we ping it */
157
158 wdt_change(WDT_ENABLE);
159
160 /* Start the timer */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100161 mod_timer(&timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Joe Perches27c766a2012-02-15 15:06:19 -0800163 pr_info("Watchdog timer is now enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166static void wdt_turnoff(void)
167{
168 /* Stop the timer */
169 del_timer_sync(&timer);
170 wdt_change(WDT_DISABLE);
Joe Perches27c766a2012-02-15 15:06:19 -0800171 pr_info("Watchdog timer is now disabled...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static void wdt_keepalive(void)
175{
176 /* user land ping */
177 next_heartbeat = jiffies + (timeout * HZ);
178}
179
180/*
181 * /dev/watchdog handling
182 */
183
Alan Cox173d95b2008-05-19 14:04:57 +0100184static ssize_t fop_write(struct file *file, const char __user *buf,
185 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 /* See if we got the magic character 'V' and reload the timer */
Alan Cox173d95b2008-05-19 14:04:57 +0100188 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!nowayout) {
190 size_t ofs;
191
192 /* note: just in case someone wrote the magic character
193 * five months ago... */
194 wdt_expect_close = 0;
195
196 /* now scan */
197 for (ofs = 0; ofs != count; ofs++) {
198 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000199 if (get_user(c, buf + ofs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EFAULT;
201 if (c == 'V')
202 wdt_expect_close = 42;
203 }
204 }
205 /* someone wrote to us, we should restart timer */
206 wdt_keepalive();
207 }
208 return count;
209}
210
Alan Cox173d95b2008-05-19 14:04:57 +0100211static int fop_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 /* Just in case we're already talking to someone... */
Alan Cox173d95b2008-05-19 14:04:57 +0100214 if (test_and_set_bit(0, &wdt_is_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -EBUSY;
216 /* Good, fire up the show */
217 wdt_startup();
218 return nonseekable_open(inode, file);
219}
220
Alan Cox173d95b2008-05-19 14:04:57 +0100221static int fop_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Alan Cox173d95b2008-05-19 14:04:57 +0100223 if (wdt_expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 wdt_turnoff();
225 else {
226 /* wim: shouldn't there be a: del_timer(&timer); */
Joe Perches27c766a2012-02-15 15:06:19 -0800227 pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 clear_bit(0, &wdt_is_open);
230 wdt_expect_close = 0;
231 return 0;
232}
233
Alan Cox173d95b2008-05-19 14:04:57 +0100234static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 void __user *argp = (void __user *)arg;
237 int __user *p = argp;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000238 static const struct watchdog_info ident = {
Alan Cox173d95b2008-05-19 14:04:57 +0100239 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
240 | WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .firmware_version = 1,
242 .identity = "ALiM7101",
243 };
244
Alan Cox173d95b2008-05-19 14:04:57 +0100245 switch (cmd) {
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);
Alan Cox173d95b2008-05-19 14:04:57 +0100251 case WDIOC_SETOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 {
Alan Cox173d95b2008-05-19 14:04:57 +0100253 int new_options, retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Alan Cox173d95b2008-05-19 14:04:57 +0100255 if (get_user(new_options, p))
256 return -EFAULT;
257 if (new_options & WDIOS_DISABLECARD) {
258 wdt_turnoff();
259 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Alan Cox173d95b2008-05-19 14:04:57 +0100261 if (new_options & WDIOS_ENABLECARD) {
262 wdt_startup();
263 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Alan Cox173d95b2008-05-19 14:04:57 +0100265 return retval;
266 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000267 case WDIOC_KEEPALIVE:
268 wdt_keepalive();
269 return 0;
Alan Cox173d95b2008-05-19 14:04:57 +0100270 case WDIOC_SETTIMEOUT:
271 {
272 int new_timeout;
273
274 if (get_user(new_timeout, p))
275 return -EFAULT;
276 /* arbitrary upper limit */
277 if (new_timeout < 1 || new_timeout > 3600)
278 return -EINVAL;
279 timeout = new_timeout;
280 wdt_keepalive();
281 /* Fall through */
282 }
283 case WDIOC_GETTIMEOUT:
284 return put_user(timeout, p);
285 default:
286 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288}
289
Arjan van de Ven62322d22006-07-03 00:24:21 -0700290static const struct file_operations wdt_fops = {
Alan Cox173d95b2008-05-19 14:04:57 +0100291 .owner = THIS_MODULE,
292 .llseek = no_llseek,
293 .write = fop_write,
294 .open = fop_open,
295 .release = fop_close,
296 .unlocked_ioctl = fop_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
299static struct miscdevice wdt_miscdev = {
Alan Cox173d95b2008-05-19 14:04:57 +0100300 .minor = WATCHDOG_MINOR,
301 .name = "watchdog",
302 .fops = &wdt_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
305/*
306 * Notifier for system down
307 */
308
Alan Cox173d95b2008-05-19 14:04:57 +0100309static int wdt_notify_sys(struct notifier_block *this,
310 unsigned long code, void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Alan Cox173d95b2008-05-19 14:04:57 +0100312 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 wdt_turnoff();
314
Alan Cox173d95b2008-05-19 14:04:57 +0100315 if (code == SYS_RESTART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
Alan Cox173d95b2008-05-19 14:04:57 +0100317 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321 wdt_change(WDT_ENABLE);
Joe Perches27c766a2012-02-15 15:06:19 -0800322 pr_info("Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 return NOTIFY_DONE;
325}
326
327/*
328 * The WDT needs to learn about soft shutdowns in order to
329 * turn the timebomb registers off.
330 */
331
Alan Cox173d95b2008-05-19 14:04:57 +0100332static struct notifier_block wdt_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .notifier_call = wdt_notify_sys,
334};
335
336static void __exit alim7101_wdt_unload(void)
337{
338 wdt_turnoff();
339 /* Deregister */
340 misc_deregister(&wdt_miscdev);
341 unregister_reboot_notifier(&wdt_notifier);
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159342 pci_dev_put(alim7101_pmu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345static int __init alim7101_wdt_init(void)
346{
347 int rc = -EBUSY;
348 struct pci_dev *ali1543_south;
349 char tmp;
350
Joe Perches27c766a2012-02-15 15:06:19 -0800351 pr_info("Steve Hill <steve@navaho.co.uk>\n");
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159352 alim7101_pmu = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
353 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!alim7101_pmu) {
Joe Perches27c766a2012-02-15 15:06:19 -0800355 pr_info("ALi M7101 PMU not present - WDT not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return -EBUSY;
357 }
358
359 /* Set the WDT in the PMU to 1 second */
360 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
361
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159362 ali1543_south = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
363 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!ali1543_south) {
Joe Perches27c766a2012-02-15 15:06:19 -0800365 pr_info("ALi 1543 South-Bridge not present - WDT not set\n");
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159366 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 pci_read_config_byte(ali1543_south, 0x5e, &tmp);
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159369 pci_dev_put(ali1543_south);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if ((tmp & 0x1e) == 0x00) {
371 if (!use_gpio) {
Joe Perches27c766a2012-02-15 15:06:19 -0800372 pr_info("Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159373 goto err_out;
Alan Cox173d95b2008-05-19 14:04:57 +0100374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 nowayout = 1;
376 } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
Joe Perches27c766a2012-02-15 15:06:19 -0800377 pr_info("ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159378 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Alan Cox173d95b2008-05-19 14:04:57 +0100381 if (timeout < 1 || timeout > 3600) {
382 /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 timeout = WATCHDOG_TIMEOUT;
Joe Perches27c766a2012-02-15 15:06:19 -0800384 pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
385 timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 rc = register_reboot_notifier(&wdt_notifier);
389 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800390 pr_err("cannot register reboot notifier (err=%d)\n", rc);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000391 goto err_out;
392 }
393
394 rc = misc_register(&wdt_miscdev);
395 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800396 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
397 wdt_miscdev.minor, rc);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000398 goto err_out_reboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Alan Cox173d95b2008-05-19 14:04:57 +0100401 if (nowayout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Joe Perches27c766a2012-02-15 15:06:19 -0800404 pr_info("WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 timeout, nowayout);
406 return 0;
407
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000408err_out_reboot:
409 unregister_reboot_notifier(&wdt_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410err_out:
Jiri Slaby02be2ee2006-07-18 18:29:00 +0159411 pci_dev_put(alim7101_pmu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return rc;
413}
414
415module_init(alim7101_wdt_init);
416module_exit(alim7101_wdt_unload);
417
Wim Van Sebroeck4562f532011-02-21 12:16:44 +0000418static DEFINE_PCI_DEVICE_TABLE(alim7101_pci_tbl) __used = {
Jiri Slaby29d73aa2007-02-12 00:51:48 -0800419 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533) },
420 { PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
Ben Collins5cacb9f2006-10-18 08:24:30 -0400421 { }
422};
423
424MODULE_DEVICE_TABLE(pci, alim7101_pci_tbl);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426MODULE_AUTHOR("Steve Hill");
427MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
428MODULE_LICENSE("GPL");
429MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);