Char: mxser, 0 to NULL in pointer
[linux-2.6/mini2440.git] / drivers / watchdog / it8712f_wdt.c
blob1b6d7d1b715d57f819994e9790ac8be57c12abf3
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 * IT8712F EC-LPC I/O Preliminary Specification 0.9.2.pdf
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * The author(s) of this software shall not be held liable for damages
18 * of any nature resulting due to the use of this software. This
19 * software is provided AS-IS with no warranties.
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/miscdevice.h>
26 #include <linux/watchdog.h>
27 #include <linux/notifier.h>
28 #include <linux/reboot.h>
29 #include <linux/fs.h>
30 #include <linux/pci.h>
31 #include <linux/spinlock.h>
33 #include <asm/uaccess.h>
34 #include <asm/io.h>
36 #define NAME "it8712f_wdt"
38 MODULE_AUTHOR("Jorge Boncompte - DTI2 <jorge@dti2.net>");
39 MODULE_DESCRIPTION("IT8712F Watchdog Driver");
40 MODULE_LICENSE("GPL");
41 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
43 static int margin = 60; /* in seconds */
44 module_param(margin, int, 0);
45 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
47 static int nowayout = WATCHDOG_NOWAYOUT;
48 module_param(nowayout, int, 0);
49 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
51 static struct semaphore it8712f_wdt_sem;
52 static unsigned expect_close;
53 static spinlock_t io_lock;
55 /* Dog Food address - We use the game port address */
56 static unsigned short address;
58 #define REG 0x2e /* The register to read/write */
59 #define VAL 0x2f /* The value to read/write */
61 #define LDN 0x07 /* Register: Logical device select */
62 #define DEVID 0x20 /* Register: Device ID */
63 #define DEVREV 0x22 /* Register: Device Revision */
64 #define ACT_REG 0x30 /* LDN Register: Activation */
65 #define BASE_REG 0x60 /* LDN Register: Base address */
67 #define IT8712F_DEVID 0x8712
69 #define LDN_GPIO 0x07 /* GPIO and Watch Dog Timer */
70 #define LDN_GAME 0x09 /* Game Port */
72 #define WDT_CONTROL 0x71 /* WDT Register: Control */
73 #define WDT_CONFIG 0x72 /* WDT Register: Configuration */
74 #define WDT_TIMEOUT 0x73 /* WDT Register: Timeout Value */
76 #define WDT_RESET_GAME 0x10
77 #define WDT_RESET_KBD 0x20
78 #define WDT_RESET_MOUSE 0x40
79 #define WDT_RESET_CIR 0x80
81 #define WDT_UNIT_SEC 0x80 /* If 0 in MINUTES */
83 #define WDT_OUT_PWROK 0x10
84 #define WDT_OUT_KRST 0x40
86 static int
87 superio_inb(int reg)
89 outb(reg, REG);
90 return inb(VAL);
93 static void
94 superio_outb(int val, int reg)
96 outb(reg, REG);
97 outb(val, VAL);
100 static int
101 superio_inw(int reg)
103 int val;
104 outb(reg++, REG);
105 val = inb(VAL) << 8;
106 outb(reg, REG);
107 val |= inb(VAL);
108 return val;
111 static inline void
112 superio_select(int ldn)
114 outb(LDN, REG);
115 outb(ldn, VAL);
118 static inline void
119 superio_enter(void)
121 spin_lock(&io_lock);
122 outb(0x87, REG);
123 outb(0x01, REG);
124 outb(0x55, REG);
125 outb(0x55, REG);
128 static inline void
129 superio_exit(void)
131 outb(0x02, REG);
132 outb(0x02, VAL);
133 spin_unlock(&io_lock);
136 static inline void
137 it8712f_wdt_ping(void)
139 inb(address);
142 static void
143 it8712f_wdt_update_margin(void)
145 int config = WDT_OUT_KRST | WDT_OUT_PWROK;
147 printk(KERN_INFO NAME ": timer margin %d seconds\n", margin);
149 /* The timeout register only has 8bits wide */
150 if (margin < 256)
151 config |= WDT_UNIT_SEC; /* else UNIT are MINUTES */
152 superio_outb(config, WDT_CONFIG);
154 superio_outb((margin > 255) ? (margin / 60) : margin, WDT_TIMEOUT);
157 static void
158 it8712f_wdt_enable(void)
160 printk(KERN_DEBUG NAME ": enabling watchdog timer\n");
161 superio_enter();
162 superio_select(LDN_GPIO);
164 superio_outb(WDT_RESET_GAME, WDT_CONTROL);
166 it8712f_wdt_update_margin();
168 superio_exit();
170 it8712f_wdt_ping();
173 static void
174 it8712f_wdt_disable(void)
176 printk(KERN_DEBUG NAME ": disabling watchdog timer\n");
178 superio_enter();
179 superio_select(LDN_GPIO);
181 superio_outb(0, WDT_CONFIG);
182 superio_outb(0, WDT_CONTROL);
183 superio_outb(0, WDT_TIMEOUT);
185 superio_exit();
188 static int
189 it8712f_wdt_notify(struct notifier_block *this,
190 unsigned long code, void *unused)
192 if (code == SYS_HALT || code == SYS_POWER_OFF)
193 if (!nowayout)
194 it8712f_wdt_disable();
196 return NOTIFY_DONE;
199 static struct notifier_block it8712f_wdt_notifier = {
200 .notifier_call = it8712f_wdt_notify,
203 static ssize_t
204 it8712f_wdt_write(struct file *file, const char __user *data,
205 size_t len, loff_t *ppos)
207 /* check for a magic close character */
208 if (len) {
209 size_t i;
211 it8712f_wdt_ping();
213 expect_close = 0;
214 for (i = 0; i < len; ++i) {
215 char c;
216 if (get_user(c, data+i))
217 return -EFAULT;
218 if (c == 'V')
219 expect_close = 42;
223 return len;
226 static int
227 it8712f_wdt_ioctl(struct inode *inode, struct file *file,
228 unsigned int cmd, unsigned long arg)
230 void __user *argp = (void __user *)arg;
231 int __user *p = argp;
232 static struct watchdog_info ident = {
233 .identity = "IT8712F Watchdog",
234 .firmware_version = 1,
235 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
237 int new_margin;
239 switch (cmd) {
240 default:
241 return -ENOTTY;
242 case WDIOC_GETSUPPORT:
243 if (copy_to_user(argp, &ident, sizeof(ident)))
244 return -EFAULT;
245 return 0;
246 case WDIOC_GETSTATUS:
247 case WDIOC_GETBOOTSTATUS:
248 return put_user(0, p);
249 case WDIOC_KEEPALIVE:
250 it8712f_wdt_ping();
251 return 0;
252 case WDIOC_SETTIMEOUT:
253 if (get_user(new_margin, p))
254 return -EFAULT;
255 if (new_margin < 1)
256 return -EINVAL;
257 margin = new_margin;
258 superio_enter();
259 superio_select(LDN_GPIO);
261 it8712f_wdt_update_margin();
263 superio_exit();
264 it8712f_wdt_ping();
265 case WDIOC_GETTIMEOUT:
266 if (put_user(margin, p))
267 return -EFAULT;
268 return 0;
272 static int
273 it8712f_wdt_open(struct inode *inode, struct file *file)
275 /* only allow one at a time */
276 if (down_trylock(&it8712f_wdt_sem))
277 return -EBUSY;
278 it8712f_wdt_enable();
280 return nonseekable_open(inode, file);
283 static int
284 it8712f_wdt_release(struct inode *inode, struct file *file)
286 if (expect_close != 42) {
287 printk(KERN_WARNING NAME
288 ": watchdog device closed unexpectedly, will not"
289 " disable the watchdog timer\n");
290 } else if (!nowayout) {
291 it8712f_wdt_disable();
293 expect_close = 0;
294 up(&it8712f_wdt_sem);
296 return 0;
299 static const struct file_operations it8712f_wdt_fops = {
300 .owner = THIS_MODULE,
301 .llseek = no_llseek,
302 .write = it8712f_wdt_write,
303 .ioctl = it8712f_wdt_ioctl,
304 .open = it8712f_wdt_open,
305 .release = it8712f_wdt_release,
308 static struct miscdevice it8712f_wdt_miscdev = {
309 .minor = WATCHDOG_MINOR,
310 .name = "watchdog",
311 .fops = &it8712f_wdt_fops,
314 static int __init
315 it8712f_wdt_find(unsigned short *address)
317 int err = -ENODEV;
318 int chip_type;
320 superio_enter();
321 chip_type = superio_inw(DEVID);
322 if (chip_type != IT8712F_DEVID)
323 goto exit;
325 superio_select(LDN_GAME);
326 superio_outb(1, ACT_REG);
327 if (!(superio_inb(ACT_REG) & 0x01)) {
328 printk(KERN_ERR NAME ": Device not activated, skipping\n");
329 goto exit;
332 *address = superio_inw(BASE_REG);
333 if (*address == 0) {
334 printk(KERN_ERR NAME ": Base address not set, skipping\n");
335 goto exit;
338 err = 0;
339 printk(KERN_DEBUG NAME ": Found IT%04xF chip revision %d - "
340 "using DogFood address 0x%x\n",
341 chip_type, superio_inb(DEVREV) & 0x0f, *address);
343 exit:
344 superio_exit();
345 return err;
348 static int __init
349 it8712f_wdt_init(void)
351 int err = 0;
353 spin_lock_init(&io_lock);
355 if (it8712f_wdt_find(&address))
356 return -ENODEV;
358 if (!request_region(address, 1, "IT8712F Watchdog")) {
359 printk(KERN_WARNING NAME ": watchdog I/O region busy\n");
360 return -EBUSY;
363 it8712f_wdt_disable();
365 sema_init(&it8712f_wdt_sem, 1);
367 err = register_reboot_notifier(&it8712f_wdt_notifier);
368 if (err) {
369 printk(KERN_ERR NAME ": unable to register reboot notifier\n");
370 goto out;
373 err = misc_register(&it8712f_wdt_miscdev);
374 if (err) {
375 printk(KERN_ERR NAME
376 ": cannot register miscdev on minor=%d (err=%d)\n",
377 WATCHDOG_MINOR, err);
378 goto reboot_out;
381 return 0;
384 reboot_out:
385 unregister_reboot_notifier(&it8712f_wdt_notifier);
386 out:
387 release_region(address, 1);
388 return err;
391 static void __exit
392 it8712f_wdt_exit(void)
394 misc_deregister(&it8712f_wdt_miscdev);
395 unregister_reboot_notifier(&it8712f_wdt_notifier);
396 release_region(address, 1);
399 module_init(it8712f_wdt_init);
400 module_exit(it8712f_wdt_exit);