2 * sch311x_wdt.c - Driver for the SCH311x Super-I/O chips
5 * (c) Copyright 2008 Wim Van Sebroeck <wim@iguana.be>.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
13 * provide warranty for any of this software. This material is
14 * provided "AS-IS" and at no charge.
18 * Includes, defines, variables, module parameters, ...
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h> /* For module specific items */
25 #include <linux/moduleparam.h> /* For new moduleparam's */
26 #include <linux/types.h> /* For standard types (like size_t) */
27 #include <linux/errno.h> /* For the -ENODEV/... values */
28 #include <linux/kernel.h> /* For printk/... */
29 #include <linux/miscdevice.h> /* For struct miscdevice */
30 #include <linux/watchdog.h> /* For the watchdog specific items */
31 #include <linux/init.h> /* For __init/__exit/... */
32 #include <linux/fs.h> /* For file operations */
33 #include <linux/platform_device.h> /* For platform_driver framework */
34 #include <linux/ioport.h> /* For io-port access */
35 #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
36 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
37 #include <linux/io.h> /* For inb/outb/... */
39 /* Module and version information */
40 #define DRV_NAME "sch311x_wdt"
42 /* Runtime registers */
44 #define WDT_TIME_OUT 0x65
49 /* internal variables */
50 static unsigned long sch311x_wdt_is_open
;
51 static char sch311x_wdt_expect_close
;
52 static struct platform_device
*sch311x_wdt_pdev
;
54 static int sch311x_ioports
[] = { 0x2e, 0x4e, 0x162e, 0x164e, 0x00 };
56 static struct { /* The devices private data */
57 /* the Runtime Register base address */
58 unsigned short runtime_reg
;
59 /* The card's boot status */
61 /* the lock for io operations */
65 /* Module load parameters */
66 static unsigned short force_id
;
67 module_param(force_id
, ushort
, 0);
68 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
70 #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
71 static int timeout
= WATCHDOG_TIMEOUT
; /* in seconds */
72 module_param(timeout
, int, 0);
73 MODULE_PARM_DESC(timeout
,
74 "Watchdog timeout in seconds. 1<= timeout <=15300, default="
75 __MODULE_STRING(WATCHDOG_TIMEOUT
) ".");
77 static bool nowayout
= WATCHDOG_NOWAYOUT
;
78 module_param(nowayout
, bool, 0);
79 MODULE_PARM_DESC(nowayout
,
80 "Watchdog cannot be stopped once started (default="
81 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
87 static inline void sch311x_sio_enter(int sio_config_port
)
89 outb(0x55, sio_config_port
);
92 static inline void sch311x_sio_exit(int sio_config_port
)
94 outb(0xaa, sio_config_port
);
97 static inline int sch311x_sio_inb(int sio_config_port
, int reg
)
99 outb(reg
, sio_config_port
);
100 return inb(sio_config_port
+ 1);
103 static inline void sch311x_sio_outb(int sio_config_port
, int reg
, int val
)
105 outb(reg
, sio_config_port
);
106 outb(val
, sio_config_port
+ 1);
110 * Watchdog Operations
113 static void sch311x_wdt_set_timeout(int t
)
115 unsigned char timeout_unit
= 0x80;
117 /* When new timeout is bigger then 255 seconds, we will use minutes */
123 /* -- Watchdog Timeout --
125 * Bit 7 WDT Time-out Value Units Select
126 * (0 = Minutes, 1 = Seconds)
128 outb(timeout_unit
, sch311x_wdt_data
.runtime_reg
+ WDT_TIME_OUT
);
130 /* -- Watchdog Timer Time-out Value --
131 * Bit 0-7 Binary coded units (0=Disabled, 1..255)
133 outb(t
, sch311x_wdt_data
.runtime_reg
+ WDT_VAL
);
136 static void sch311x_wdt_start(void)
140 spin_lock(&sch311x_wdt_data
.io_lock
);
142 /* set watchdog's timeout */
143 sch311x_wdt_set_timeout(timeout
);
144 /* enable the watchdog */
145 /* -- General Purpose I/O Bit 6.0 --
146 * Bit 0, In/Out: 0 = Output, 1 = Input
147 * Bit 1, Polarity: 0 = No Invert, 1 = Invert
148 * Bit 2-3, Function select: 00 = GPI/O, 01 = LED1, 11 = WDT,
149 * 10 = Either Edge Triggered Intr.4
151 * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
153 t
= inb(sch311x_wdt_data
.runtime_reg
+ GP60
);
154 outb((t
& ~0x0d) | 0x0c, sch311x_wdt_data
.runtime_reg
+ GP60
);
156 spin_unlock(&sch311x_wdt_data
.io_lock
);
160 static void sch311x_wdt_stop(void)
164 spin_lock(&sch311x_wdt_data
.io_lock
);
166 /* stop the watchdog */
167 t
= inb(sch311x_wdt_data
.runtime_reg
+ GP60
);
168 outb((t
& ~0x0d) | 0x01, sch311x_wdt_data
.runtime_reg
+ GP60
);
169 /* disable timeout by setting it to 0 */
170 sch311x_wdt_set_timeout(0);
172 spin_unlock(&sch311x_wdt_data
.io_lock
);
175 static void sch311x_wdt_keepalive(void)
177 spin_lock(&sch311x_wdt_data
.io_lock
);
178 sch311x_wdt_set_timeout(timeout
);
179 spin_unlock(&sch311x_wdt_data
.io_lock
);
182 static int sch311x_wdt_set_heartbeat(int t
)
184 if (t
< 1 || t
> (255*60))
187 /* When new timeout is bigger then 255 seconds,
188 * we will round up to minutes (with a max of 255) */
190 t
= (((t
- 1) / 60) + 1) * 60;
196 static void sch311x_wdt_get_status(int *status
)
198 unsigned char new_status
;
202 spin_lock(&sch311x_wdt_data
.io_lock
);
204 /* -- Watchdog timer control --
205 * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
207 * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
208 * Bit 3 P20 Force Timeout enabled:
209 * 0 = P20 activity does not generate the WD timeout event
210 * 1 = P20 Allows rising edge of P20, from the keyboard
211 * controller, to force the WD timeout event.
214 new_status
= inb(sch311x_wdt_data
.runtime_reg
+ WDT_CTRL
);
215 if (new_status
& 0x01)
216 *status
|= WDIOF_CARDRESET
;
218 spin_unlock(&sch311x_wdt_data
.io_lock
);
222 * /dev/watchdog handling
225 static ssize_t
sch311x_wdt_write(struct file
*file
, const char __user
*buf
,
226 size_t count
, loff_t
*ppos
)
232 sch311x_wdt_expect_close
= 0;
234 for (i
= 0; i
!= count
; i
++) {
236 if (get_user(c
, buf
+ i
))
239 sch311x_wdt_expect_close
= 42;
242 sch311x_wdt_keepalive();
247 static long sch311x_wdt_ioctl(struct file
*file
, unsigned int cmd
,
252 void __user
*argp
= (void __user
*)arg
;
253 int __user
*p
= argp
;
254 static const struct watchdog_info ident
= {
255 .options
= WDIOF_KEEPALIVEPING
|
258 .firmware_version
= 1,
259 .identity
= DRV_NAME
,
263 case WDIOC_GETSUPPORT
:
264 if (copy_to_user(argp
, &ident
, sizeof(ident
)))
268 case WDIOC_GETSTATUS
:
270 sch311x_wdt_get_status(&status
);
271 return put_user(status
, p
);
273 case WDIOC_GETBOOTSTATUS
:
274 return put_user(sch311x_wdt_data
.boot_status
, p
);
276 case WDIOC_SETOPTIONS
:
278 int options
, retval
= -EINVAL
;
280 if (get_user(options
, p
))
282 if (options
& WDIOS_DISABLECARD
) {
286 if (options
& WDIOS_ENABLECARD
) {
292 case WDIOC_KEEPALIVE
:
293 sch311x_wdt_keepalive();
296 case WDIOC_SETTIMEOUT
:
297 if (get_user(new_timeout
, p
))
299 if (sch311x_wdt_set_heartbeat(new_timeout
))
301 sch311x_wdt_keepalive();
303 case WDIOC_GETTIMEOUT
:
304 return put_user(timeout
, p
);
311 static int sch311x_wdt_open(struct inode
*inode
, struct file
*file
)
313 if (test_and_set_bit(0, &sch311x_wdt_is_open
))
319 return nonseekable_open(inode
, file
);
322 static int sch311x_wdt_close(struct inode
*inode
, struct file
*file
)
324 if (sch311x_wdt_expect_close
== 42) {
327 pr_crit("Unexpected close, not stopping watchdog!\n");
328 sch311x_wdt_keepalive();
330 clear_bit(0, &sch311x_wdt_is_open
);
331 sch311x_wdt_expect_close
= 0;
339 static const struct file_operations sch311x_wdt_fops
= {
340 .owner
= THIS_MODULE
,
342 .write
= sch311x_wdt_write
,
343 .unlocked_ioctl
= sch311x_wdt_ioctl
,
344 .open
= sch311x_wdt_open
,
345 .release
= sch311x_wdt_close
,
348 static struct miscdevice sch311x_wdt_miscdev
= {
349 .minor
= WATCHDOG_MINOR
,
351 .fops
= &sch311x_wdt_fops
,
355 * Init & exit routines
358 static int sch311x_wdt_probe(struct platform_device
*pdev
)
360 struct device
*dev
= &pdev
->dev
;
363 spin_lock_init(&sch311x_wdt_data
.io_lock
);
365 if (!request_region(sch311x_wdt_data
.runtime_reg
+ GP60
, 1, DRV_NAME
)) {
366 dev_err(dev
, "Failed to request region 0x%04x-0x%04x.\n",
367 sch311x_wdt_data
.runtime_reg
+ GP60
,
368 sch311x_wdt_data
.runtime_reg
+ GP60
);
373 if (!request_region(sch311x_wdt_data
.runtime_reg
+ WDT_TIME_OUT
, 4,
375 dev_err(dev
, "Failed to request region 0x%04x-0x%04x.\n",
376 sch311x_wdt_data
.runtime_reg
+ WDT_TIME_OUT
,
377 sch311x_wdt_data
.runtime_reg
+ WDT_CTRL
);
379 goto exit_release_region
;
382 /* Make sure that the watchdog is not running */
385 /* Disable keyboard and mouse interaction and interrupt */
386 /* -- Watchdog timer configuration --
388 * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
389 * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
391 * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
392 * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
394 outb(0, sch311x_wdt_data
.runtime_reg
+ WDT_CFG
);
396 /* Check that the heartbeat value is within it's range ;
397 * if not reset to the default */
398 if (sch311x_wdt_set_heartbeat(timeout
)) {
399 sch311x_wdt_set_heartbeat(WATCHDOG_TIMEOUT
);
400 dev_info(dev
, "timeout value must be 1<=x<=15300, using %d\n",
404 /* Get status at boot */
405 sch311x_wdt_get_status(&sch311x_wdt_data
.boot_status
);
407 sch311x_wdt_miscdev
.parent
= dev
;
409 err
= misc_register(&sch311x_wdt_miscdev
);
411 dev_err(dev
, "cannot register miscdev on minor=%d (err=%d)\n",
412 WATCHDOG_MINOR
, err
);
413 goto exit_release_region2
;
417 "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n",
422 exit_release_region2
:
423 release_region(sch311x_wdt_data
.runtime_reg
+ WDT_TIME_OUT
, 4);
425 release_region(sch311x_wdt_data
.runtime_reg
+ GP60
, 1);
426 sch311x_wdt_data
.runtime_reg
= 0;
431 static int sch311x_wdt_remove(struct platform_device
*pdev
)
433 /* Stop the timer before we leave */
438 misc_deregister(&sch311x_wdt_miscdev
);
439 release_region(sch311x_wdt_data
.runtime_reg
+ WDT_TIME_OUT
, 4);
440 release_region(sch311x_wdt_data
.runtime_reg
+ GP60
, 1);
441 sch311x_wdt_data
.runtime_reg
= 0;
445 static void sch311x_wdt_shutdown(struct platform_device
*dev
)
447 /* Turn the WDT off if we have a soft shutdown */
451 static struct platform_driver sch311x_wdt_driver
= {
452 .probe
= sch311x_wdt_probe
,
453 .remove
= sch311x_wdt_remove
,
454 .shutdown
= sch311x_wdt_shutdown
,
456 .owner
= THIS_MODULE
,
461 static int __init
sch311x_detect(int sio_config_port
, unsigned short *addr
)
464 unsigned short base_addr
;
465 unsigned char dev_id
;
467 sch311x_sio_enter(sio_config_port
);
469 /* Check device ID. We currently know about:
470 * SCH3112 (0x7c), SCH3114 (0x7d), and SCH3116 (0x7f). */
471 reg
= force_id
? force_id
: sch311x_sio_inb(sio_config_port
, 0x20);
472 if (!(reg
== 0x7c || reg
== 0x7d || reg
== 0x7f)) {
476 dev_id
= reg
== 0x7c ? 2 : reg
== 0x7d ? 4 : 6;
478 /* Select logical device A (runtime registers) */
479 sch311x_sio_outb(sio_config_port
, 0x07, 0x0a);
481 /* Check if Logical Device Register is currently active */
482 if ((sch311x_sio_inb(sio_config_port
, 0x30) & 0x01) == 0)
483 pr_info("Seems that LDN 0x0a is not active...\n");
485 /* Get the base address of the runtime registers */
486 base_addr
= (sch311x_sio_inb(sio_config_port
, 0x60) << 8) |
487 sch311x_sio_inb(sio_config_port
, 0x61);
489 pr_err("Base address not set\n");
495 pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id
, base_addr
);
498 sch311x_sio_exit(sio_config_port
);
502 static int __init
sch311x_wdt_init(void)
504 int err
, i
, found
= 0;
505 unsigned short addr
= 0;
507 for (i
= 0; !found
&& sch311x_ioports
[i
]; i
++)
508 if (sch311x_detect(sch311x_ioports
[i
], &addr
) == 0)
514 sch311x_wdt_data
.runtime_reg
= addr
;
516 err
= platform_driver_register(&sch311x_wdt_driver
);
520 sch311x_wdt_pdev
= platform_device_register_simple(DRV_NAME
, addr
,
523 if (IS_ERR(sch311x_wdt_pdev
)) {
524 err
= PTR_ERR(sch311x_wdt_pdev
);
525 goto unreg_platform_driver
;
530 unreg_platform_driver
:
531 platform_driver_unregister(&sch311x_wdt_driver
);
535 static void __exit
sch311x_wdt_exit(void)
537 platform_device_unregister(sch311x_wdt_pdev
);
538 platform_driver_unregister(&sch311x_wdt_driver
);
541 module_init(sch311x_wdt_init
);
542 module_exit(sch311x_wdt_exit
);
544 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
545 MODULE_DESCRIPTION("SMSC SCH311x WatchDog Timer Driver");
546 MODULE_LICENSE("GPL");