Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / watchdog / it8712f_wdt.c
blob7adbfd60651a7ff723666d6dba26f45dc34b6eca
1 /*
2 * IT8712F "Smart Guardian" Watchdog support
4 * Copyright (c) 2006-2007 Jorge Boncompte - DTI2 <jorge@dti2.net>
6 * Based on info and code taken from:
8 * drivers/char/watchdog/scx200_wdt.c
9 * drivers/hwmon/it87.c
10 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
11 * IT8712F EC-LPC I/O Preliminary Specification 0.9.2.pdf
12 =======
13 * IT8712F EC-LPC I/O Preliminary Specification 0.8.2
14 * IT8712F EC-LPC I/O Preliminary Specification 0.9.3
15 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of the
20 * License, or (at your option) any later version.
22 * The author(s) of this software shall not be held liable for damages
23 * of any nature resulting due to the use of this software. This
24 * software is provided AS-IS with no warranties.
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/miscdevice.h>
31 #include <linux/watchdog.h>
32 #include <linux/notifier.h>
33 #include <linux/reboot.h>
34 #include <linux/fs.h>
35 #include <linux/pci.h>
36 #include <linux/spinlock.h>
38 #include <asm/uaccess.h>
39 #include <asm/io.h>
41 #define NAME "it8712f_wdt"
43 MODULE_AUTHOR("Jorge Boncompte - DTI2 <jorge@dti2.net>");
44 MODULE_DESCRIPTION("IT8712F Watchdog Driver");
45 MODULE_LICENSE("GPL");
46 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
48 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
49 =======
50 static int max_units = 255;
51 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
52 static int margin = 60; /* in seconds */
53 module_param(margin, int, 0);
54 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
56 static int nowayout = WATCHDOG_NOWAYOUT;
57 module_param(nowayout, int, 0);
58 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
60 static struct semaphore it8712f_wdt_sem;
61 static unsigned expect_close;
62 static spinlock_t io_lock;
63 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
64 =======
65 static unsigned char revision;
66 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
68 /* Dog Food address - We use the game port address */
69 static unsigned short address;
71 #define REG 0x2e /* The register to read/write */
72 #define VAL 0x2f /* The value to read/write */
74 #define LDN 0x07 /* Register: Logical device select */
75 #define DEVID 0x20 /* Register: Device ID */
76 #define DEVREV 0x22 /* Register: Device Revision */
77 #define ACT_REG 0x30 /* LDN Register: Activation */
78 #define BASE_REG 0x60 /* LDN Register: Base address */
80 #define IT8712F_DEVID 0x8712
82 #define LDN_GPIO 0x07 /* GPIO and Watch Dog Timer */
83 #define LDN_GAME 0x09 /* Game Port */
85 #define WDT_CONTROL 0x71 /* WDT Register: Control */
86 #define WDT_CONFIG 0x72 /* WDT Register: Configuration */
87 #define WDT_TIMEOUT 0x73 /* WDT Register: Timeout Value */
89 #define WDT_RESET_GAME 0x10
90 #define WDT_RESET_KBD 0x20
91 #define WDT_RESET_MOUSE 0x40
92 #define WDT_RESET_CIR 0x80
94 #define WDT_UNIT_SEC 0x80 /* If 0 in MINUTES */
96 #define WDT_OUT_PWROK 0x10
97 #define WDT_OUT_KRST 0x40
99 static int
100 superio_inb(int reg)
102 outb(reg, REG);
103 return inb(VAL);
106 static void
107 superio_outb(int val, int reg)
109 outb(reg, REG);
110 outb(val, VAL);
113 static int
114 superio_inw(int reg)
116 int val;
117 outb(reg++, REG);
118 val = inb(VAL) << 8;
119 outb(reg, REG);
120 val |= inb(VAL);
121 return val;
124 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
125 =======
126 static void
127 superio_outw(int val, int reg)
129 outb(reg++, REG);
130 outb((val >> 8) & 0xff, VAL);
131 outb(reg, REG);
132 outb(val & 0xff, VAL);
135 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
136 static inline void
137 superio_select(int ldn)
139 outb(LDN, REG);
140 outb(ldn, VAL);
143 static inline void
144 superio_enter(void)
146 spin_lock(&io_lock);
147 outb(0x87, REG);
148 outb(0x01, REG);
149 outb(0x55, REG);
150 outb(0x55, REG);
153 static inline void
154 superio_exit(void)
156 outb(0x02, REG);
157 outb(0x02, VAL);
158 spin_unlock(&io_lock);
161 static inline void
162 it8712f_wdt_ping(void)
164 inb(address);
167 static void
168 it8712f_wdt_update_margin(void)
170 int config = WDT_OUT_KRST | WDT_OUT_PWROK;
171 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
173 printk(KERN_INFO NAME ": timer margin %d seconds\n", margin);
175 /* The timeout register only has 8bits wide */
176 if (margin < 256)
177 config |= WDT_UNIT_SEC; /* else UNIT are MINUTES */
178 =======
179 int units = margin;
181 /* Switch to minutes precision if the configured margin
182 * value does not fit within the register width.
184 if (units <= max_units) {
185 config |= WDT_UNIT_SEC; /* else UNIT is MINUTES */
186 printk(KERN_INFO NAME ": timer margin %d seconds\n", units);
187 } else {
188 units /= 60;
189 printk(KERN_INFO NAME ": timer margin %d minutes\n", units);
191 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
192 superio_outb(config, WDT_CONFIG);
194 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
195 superio_outb((margin > 255) ? (margin / 60) : margin, WDT_TIMEOUT);
196 =======
197 if (revision >= 0x08)
198 superio_outw(units, WDT_TIMEOUT);
199 else
200 superio_outb(units, WDT_TIMEOUT);
203 static int
204 it8712f_wdt_get_status(void)
206 if (superio_inb(WDT_CONTROL) & 0x01)
207 return WDIOF_CARDRESET;
208 else
209 return 0;
210 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
213 static void
214 it8712f_wdt_enable(void)
216 printk(KERN_DEBUG NAME ": enabling watchdog timer\n");
217 superio_enter();
218 superio_select(LDN_GPIO);
220 superio_outb(WDT_RESET_GAME, WDT_CONTROL);
222 it8712f_wdt_update_margin();
224 superio_exit();
226 it8712f_wdt_ping();
229 static void
230 it8712f_wdt_disable(void)
232 printk(KERN_DEBUG NAME ": disabling watchdog timer\n");
234 superio_enter();
235 superio_select(LDN_GPIO);
237 superio_outb(0, WDT_CONFIG);
238 superio_outb(0, WDT_CONTROL);
239 superio_outb(0, WDT_TIMEOUT);
241 superio_exit();
244 static int
245 it8712f_wdt_notify(struct notifier_block *this,
246 unsigned long code, void *unused)
248 if (code == SYS_HALT || code == SYS_POWER_OFF)
249 if (!nowayout)
250 it8712f_wdt_disable();
252 return NOTIFY_DONE;
255 static struct notifier_block it8712f_wdt_notifier = {
256 .notifier_call = it8712f_wdt_notify,
259 static ssize_t
260 it8712f_wdt_write(struct file *file, const char __user *data,
261 size_t len, loff_t *ppos)
263 /* check for a magic close character */
264 if (len) {
265 size_t i;
267 it8712f_wdt_ping();
269 expect_close = 0;
270 for (i = 0; i < len; ++i) {
271 char c;
272 if (get_user(c, data+i))
273 return -EFAULT;
274 if (c == 'V')
275 expect_close = 42;
279 return len;
282 static int
283 it8712f_wdt_ioctl(struct inode *inode, struct file *file,
284 unsigned int cmd, unsigned long arg)
286 void __user *argp = (void __user *)arg;
287 int __user *p = argp;
288 static struct watchdog_info ident = {
289 .identity = "IT8712F Watchdog",
290 .firmware_version = 1,
291 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
293 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
294 int new_margin;
295 =======
296 int value;
297 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
299 switch (cmd) {
300 default:
301 return -ENOTTY;
302 case WDIOC_GETSUPPORT:
303 if (copy_to_user(argp, &ident, sizeof(ident)))
304 return -EFAULT;
305 return 0;
306 case WDIOC_GETSTATUS:
307 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
308 =======
309 superio_enter();
310 superio_select(LDN_GPIO);
312 value = it8712f_wdt_get_status();
314 superio_exit();
316 return put_user(value, p);
317 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
318 case WDIOC_GETBOOTSTATUS:
319 return put_user(0, p);
320 case WDIOC_KEEPALIVE:
321 it8712f_wdt_ping();
322 return 0;
323 case WDIOC_SETTIMEOUT:
324 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
325 if (get_user(new_margin, p))
326 =======
327 if (get_user(value, p))
328 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
329 return -EFAULT;
330 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
331 if (new_margin < 1)
332 =======
333 if (value < 1)
334 return -EINVAL;
335 if (value > (max_units * 60))
336 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
337 return -EINVAL;
338 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
339 margin = new_margin;
340 =======
341 margin = value;
342 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
343 superio_enter();
344 superio_select(LDN_GPIO);
346 it8712f_wdt_update_margin();
348 superio_exit();
349 it8712f_wdt_ping();
350 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
351 =======
352 /* Fall through */
353 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
354 case WDIOC_GETTIMEOUT:
355 if (put_user(margin, p))
356 return -EFAULT;
357 return 0;
361 static int
362 it8712f_wdt_open(struct inode *inode, struct file *file)
364 /* only allow one at a time */
365 if (down_trylock(&it8712f_wdt_sem))
366 return -EBUSY;
367 it8712f_wdt_enable();
369 return nonseekable_open(inode, file);
372 static int
373 it8712f_wdt_release(struct inode *inode, struct file *file)
375 if (expect_close != 42) {
376 printk(KERN_WARNING NAME
377 ": watchdog device closed unexpectedly, will not"
378 " disable the watchdog timer\n");
379 } else if (!nowayout) {
380 it8712f_wdt_disable();
382 expect_close = 0;
383 up(&it8712f_wdt_sem);
385 return 0;
388 static const struct file_operations it8712f_wdt_fops = {
389 .owner = THIS_MODULE,
390 .llseek = no_llseek,
391 .write = it8712f_wdt_write,
392 .ioctl = it8712f_wdt_ioctl,
393 .open = it8712f_wdt_open,
394 .release = it8712f_wdt_release,
397 static struct miscdevice it8712f_wdt_miscdev = {
398 .minor = WATCHDOG_MINOR,
399 .name = "watchdog",
400 .fops = &it8712f_wdt_fops,
403 static int __init
404 it8712f_wdt_find(unsigned short *address)
406 int err = -ENODEV;
407 int chip_type;
409 superio_enter();
410 chip_type = superio_inw(DEVID);
411 if (chip_type != IT8712F_DEVID)
412 goto exit;
414 superio_select(LDN_GAME);
415 superio_outb(1, ACT_REG);
416 if (!(superio_inb(ACT_REG) & 0x01)) {
417 printk(KERN_ERR NAME ": Device not activated, skipping\n");
418 goto exit;
421 *address = superio_inw(BASE_REG);
422 if (*address == 0) {
423 printk(KERN_ERR NAME ": Base address not set, skipping\n");
424 goto exit;
427 err = 0;
428 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
429 printk(KERN_DEBUG NAME ": Found IT%04xF chip revision %d - "
430 =======
431 revision = superio_inb(DEVREV) & 0x0f;
433 /* Later revisions have 16-bit values per datasheet 0.9.1 */
434 if (revision >= 0x08)
435 max_units = 65535;
437 if (margin > (max_units * 60))
438 margin = (max_units * 60);
440 printk(KERN_INFO NAME ": Found IT%04xF chip revision %d - "
441 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
442 "using DogFood address 0x%x\n",
443 <<<<<<< HEAD:drivers/watchdog/it8712f_wdt.c
444 chip_type, superio_inb(DEVREV) & 0x0f, *address);
445 =======
446 chip_type, revision, *address);
447 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/it8712f_wdt.c
449 exit:
450 superio_exit();
451 return err;
454 static int __init
455 it8712f_wdt_init(void)
457 int err = 0;
459 spin_lock_init(&io_lock);
461 if (it8712f_wdt_find(&address))
462 return -ENODEV;
464 if (!request_region(address, 1, "IT8712F Watchdog")) {
465 printk(KERN_WARNING NAME ": watchdog I/O region busy\n");
466 return -EBUSY;
469 it8712f_wdt_disable();
471 sema_init(&it8712f_wdt_sem, 1);
473 err = register_reboot_notifier(&it8712f_wdt_notifier);
474 if (err) {
475 printk(KERN_ERR NAME ": unable to register reboot notifier\n");
476 goto out;
479 err = misc_register(&it8712f_wdt_miscdev);
480 if (err) {
481 printk(KERN_ERR NAME
482 ": cannot register miscdev on minor=%d (err=%d)\n",
483 WATCHDOG_MINOR, err);
484 goto reboot_out;
487 return 0;
490 reboot_out:
491 unregister_reboot_notifier(&it8712f_wdt_notifier);
492 out:
493 release_region(address, 1);
494 return err;
497 static void __exit
498 it8712f_wdt_exit(void)
500 misc_deregister(&it8712f_wdt_miscdev);
501 unregister_reboot_notifier(&it8712f_wdt_notifier);
502 release_region(address, 1);
505 module_init(it8712f_wdt_init);
506 module_exit(it8712f_wdt_exit);