1 // SPDX-License-Identifier: GPL-2.0+
3 * sch311x_wdt.c - Driver for the SCH311x Super-I/O chips
6 * (c) Copyright 2008 Wim Van Sebroeck <wim@iguana.be>.
8 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
9 * provide warranty for any of this software. This material is
10 * provided "AS-IS" and at no charge.
14 * Includes, defines, variables, module parameters, ...
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h> /* For module specific items */
21 #include <linux/moduleparam.h> /* For new moduleparam's */
22 #include <linux/types.h> /* For standard types (like size_t) */
23 #include <linux/errno.h> /* For the -ENODEV/... values */
24 #include <linux/kernel.h> /* For printk/... */
25 #include <linux/miscdevice.h> /* For struct miscdevice */
26 #include <linux/watchdog.h> /* For the watchdog specific items */
27 #include <linux/init.h> /* For __init/__exit/... */
28 #include <linux/fs.h> /* For file operations */
29 #include <linux/platform_device.h> /* For platform_driver framework */
30 #include <linux/ioport.h> /* For io-port access */
31 #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
32 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
33 #include <linux/io.h> /* For inb/outb/... */
35 /* Module and version information */
36 #define DRV_NAME "sch311x_wdt"
38 /* Runtime registers */
40 #define WDT_TIME_OUT 0x65
45 /* internal variables */
46 static unsigned long sch311x_wdt_is_open;
47 static char sch311x_wdt_expect_close;
48 static struct platform_device *sch311x_wdt_pdev;
50 static int sch311x_ioports[] = { 0x2e, 0x4e, 0x162e, 0x164e, 0x00 };
52 static struct { /* The devices private data */
53 /* the Runtime Register base address */
54 unsigned short runtime_reg;
55 /* The card's boot status */
57 /* the lock for io operations */
61 /* Module load parameters */
62 static unsigned short force_id;
63 module_param(force_id, ushort, 0);
64 MODULE_PARM_DESC(force_id, "Override the detected device ID");
66 #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
67 static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
68 module_param(timeout, int, 0);
69 MODULE_PARM_DESC(timeout,
70 "Watchdog timeout in seconds. 1<= timeout <=15300, default="
71 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
73 static bool nowayout = WATCHDOG_NOWAYOUT;
74 module_param(nowayout, bool, 0);
75 MODULE_PARM_DESC(nowayout,
76 "Watchdog cannot be stopped once started (default="
77 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
83 static inline void sch311x_sio_enter(int sio_config_port)
85 outb(0x55, sio_config_port);
88 static inline void sch311x_sio_exit(int sio_config_port)
90 outb(0xaa, sio_config_port);
93 static inline int sch311x_sio_inb(int sio_config_port, int reg)
95 outb(reg, sio_config_port);
96 return inb(sio_config_port + 1);
99 static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
101 outb(reg, sio_config_port);
102 outb(val, sio_config_port + 1);
106 * Watchdog Operations
109 static void sch311x_wdt_set_timeout(int t)
111 unsigned char timeout_unit = 0x80;
113 /* When new timeout is bigger then 255 seconds, we will use minutes */
119 /* -- Watchdog Timeout --
121 * Bit 7 WDT Time-out Value Units Select
122 * (0 = Minutes, 1 = Seconds)
124 outb(timeout_unit, sch311x_wdt_data.runtime_reg + WDT_TIME_OUT);
126 /* -- Watchdog Timer Time-out Value --
127 * Bit 0-7 Binary coded units (0=Disabled, 1..255)
129 outb(t, sch311x_wdt_data.runtime_reg + WDT_VAL);
132 static void sch311x_wdt_start(void)
136 spin_lock(&sch311x_wdt_data.io_lock);
138 /* set watchdog's timeout */
139 sch311x_wdt_set_timeout(timeout);
140 /* enable the watchdog */
141 /* -- General Purpose I/O Bit 6.0 --
142 * Bit 0, In/Out: 0 = Output, 1 = Input
143 * Bit 1, Polarity: 0 = No Invert, 1 = Invert
144 * Bit 2-3, Function select: 00 = GPI/O, 01 = LED1, 11 = WDT,
145 * 10 = Either Edge Triggered Intr.4
147 * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
149 t = inb(sch311x_wdt_data.runtime_reg + GP60);
150 outb((t & ~0x0d) | 0x0c, sch311x_wdt_data.runtime_reg + GP60);
152 spin_unlock(&sch311x_wdt_data.io_lock);
156 static void sch311x_wdt_stop(void)
160 spin_lock(&sch311x_wdt_data.io_lock);
162 /* stop the watchdog */
163 t = inb(sch311x_wdt_data.runtime_reg + GP60);
164 outb((t & ~0x0d) | 0x01, sch311x_wdt_data.runtime_reg + GP60);
165 /* disable timeout by setting it to 0 */
166 sch311x_wdt_set_timeout(0);
168 spin_unlock(&sch311x_wdt_data.io_lock);
171 static void sch311x_wdt_keepalive(void)
173 spin_lock(&sch311x_wdt_data.io_lock);
174 sch311x_wdt_set_timeout(timeout);
175 spin_unlock(&sch311x_wdt_data.io_lock);
178 static int sch311x_wdt_set_heartbeat(int t)
180 if (t < 1 || t > (255*60))
183 /* When new timeout is bigger then 255 seconds,
184 * we will round up to minutes (with a max of 255) */
186 t = (((t - 1) / 60) + 1) * 60;
192 static void sch311x_wdt_get_status(int *status)
194 unsigned char new_status;
198 spin_lock(&sch311x_wdt_data.io_lock);
200 /* -- Watchdog timer control --
201 * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
203 * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
204 * Bit 3 P20 Force Timeout enabled:
205 * 0 = P20 activity does not generate the WD timeout event
206 * 1 = P20 Allows rising edge of P20, from the keyboard
207 * controller, to force the WD timeout event.
210 new_status = inb(sch311x_wdt_data.runtime_reg + WDT_CTRL);
211 if (new_status & 0x01)
212 *status |= WDIOF_CARDRESET;
214 spin_unlock(&sch311x_wdt_data.io_lock);
218 * /dev/watchdog handling
221 static ssize_t sch311x_wdt_write(struct file *file, const char __user *buf,
222 size_t count, loff_t *ppos)
228 sch311x_wdt_expect_close = 0;
230 for (i = 0; i != count; i++) {
232 if (get_user(c, buf + i))
235 sch311x_wdt_expect_close = 42;
238 sch311x_wdt_keepalive();
243 static long sch311x_wdt_ioctl(struct file *file, unsigned int cmd,
248 void __user *argp = (void __user *)arg;
249 int __user *p = argp;
250 static const struct watchdog_info ident = {
251 .options = WDIOF_KEEPALIVEPING |
254 .firmware_version = 1,
255 .identity = DRV_NAME,
259 case WDIOC_GETSUPPORT:
260 if (copy_to_user(argp, &ident, sizeof(ident)))
264 case WDIOC_GETSTATUS:
266 sch311x_wdt_get_status(&status);
267 return put_user(status, p);
269 case WDIOC_GETBOOTSTATUS:
270 return put_user(sch311x_wdt_data.boot_status, p);
272 case WDIOC_SETOPTIONS:
274 int options, retval = -EINVAL;
276 if (get_user(options, p))
278 if (options & WDIOS_DISABLECARD) {
282 if (options & WDIOS_ENABLECARD) {
288 case WDIOC_KEEPALIVE:
289 sch311x_wdt_keepalive();
292 case WDIOC_SETTIMEOUT:
293 if (get_user(new_timeout, p))
295 if (sch311x_wdt_set_heartbeat(new_timeout))
297 sch311x_wdt_keepalive();
299 case WDIOC_GETTIMEOUT:
300 return put_user(timeout, p);
307 static int sch311x_wdt_open(struct inode *inode, struct file *file)
309 if (test_and_set_bit(0, &sch311x_wdt_is_open))
315 return stream_open(inode, file);
318 static int sch311x_wdt_close(struct inode *inode, struct file *file)
320 if (sch311x_wdt_expect_close == 42) {
323 pr_crit("Unexpected close, not stopping watchdog!\n");
324 sch311x_wdt_keepalive();
326 clear_bit(0, &sch311x_wdt_is_open);
327 sch311x_wdt_expect_close = 0;
335 static const struct file_operations sch311x_wdt_fops = {
336 .owner = THIS_MODULE,
338 .write = sch311x_wdt_write,
339 .unlocked_ioctl = sch311x_wdt_ioctl,
340 .compat_ioctl = compat_ptr_ioctl,
341 .open = sch311x_wdt_open,
342 .release = sch311x_wdt_close,
345 static struct miscdevice sch311x_wdt_miscdev = {
346 .minor = WATCHDOG_MINOR,
348 .fops = &sch311x_wdt_fops,
352 * Init & exit routines
355 static int sch311x_wdt_probe(struct platform_device *pdev)
357 struct device *dev = &pdev->dev;
360 spin_lock_init(&sch311x_wdt_data.io_lock);
362 if (!request_region(sch311x_wdt_data.runtime_reg + GP60, 1, DRV_NAME)) {
363 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
364 sch311x_wdt_data.runtime_reg + GP60,
365 sch311x_wdt_data.runtime_reg + GP60);
370 if (!request_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4,
372 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
373 sch311x_wdt_data.runtime_reg + WDT_TIME_OUT,
374 sch311x_wdt_data.runtime_reg + WDT_CTRL);
376 goto exit_release_region;
379 /* Make sure that the watchdog is not running */
382 /* Disable keyboard and mouse interaction and interrupt */
383 /* -- Watchdog timer configuration --
385 * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
386 * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
388 * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
389 * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
391 outb(0, sch311x_wdt_data.runtime_reg + WDT_CFG);
393 /* Check that the heartbeat value is within it's range ;
394 * if not reset to the default */
395 if (sch311x_wdt_set_heartbeat(timeout)) {
396 sch311x_wdt_set_heartbeat(WATCHDOG_TIMEOUT);
397 dev_info(dev, "timeout value must be 1<=x<=15300, using %d\n",
401 /* Get status at boot */
402 sch311x_wdt_get_status(&sch311x_wdt_data.boot_status);
404 sch311x_wdt_miscdev.parent = dev;
406 err = misc_register(&sch311x_wdt_miscdev);
408 dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n",
409 WATCHDOG_MINOR, err);
410 goto exit_release_region2;
414 "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n",
419 exit_release_region2:
420 release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
422 release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
423 sch311x_wdt_data.runtime_reg = 0;
428 static int sch311x_wdt_remove(struct platform_device *pdev)
430 /* Stop the timer before we leave */
435 misc_deregister(&sch311x_wdt_miscdev);
436 release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
437 release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
438 sch311x_wdt_data.runtime_reg = 0;
442 static void sch311x_wdt_shutdown(struct platform_device *dev)
444 /* Turn the WDT off if we have a soft shutdown */
448 static struct platform_driver sch311x_wdt_driver = {
449 .probe = sch311x_wdt_probe,
450 .remove = sch311x_wdt_remove,
451 .shutdown = sch311x_wdt_shutdown,
457 static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
460 unsigned short base_addr;
461 unsigned char dev_id;
463 sch311x_sio_enter(sio_config_port);
465 /* Check device ID. We currently know about:
466 * SCH3112 (0x7c), SCH3114 (0x7d), and SCH3116 (0x7f). */
467 reg = force_id ? force_id : sch311x_sio_inb(sio_config_port, 0x20);
468 if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
472 dev_id = reg == 0x7c ? 2 : reg == 0x7d ? 4 : 6;
474 /* Select logical device A (runtime registers) */
475 sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
477 /* Check if Logical Device Register is currently active */
478 if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
479 pr_info("Seems that LDN 0x0a is not active...\n");
481 /* Get the base address of the runtime registers */
482 base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
483 sch311x_sio_inb(sio_config_port, 0x61);
485 pr_err("Base address not set\n");
491 pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id, base_addr);
494 sch311x_sio_exit(sio_config_port);
498 static int __init sch311x_wdt_init(void)
500 int err, i, found = 0;
501 unsigned short addr = 0;
503 for (i = 0; !found && sch311x_ioports[i]; i++)
504 if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
510 sch311x_wdt_data.runtime_reg = addr;
512 err = platform_driver_register(&sch311x_wdt_driver);
516 sch311x_wdt_pdev = platform_device_register_simple(DRV_NAME, addr,
519 if (IS_ERR(sch311x_wdt_pdev)) {
520 err = PTR_ERR(sch311x_wdt_pdev);
521 goto unreg_platform_driver;
526 unreg_platform_driver:
527 platform_driver_unregister(&sch311x_wdt_driver);
531 static void __exit sch311x_wdt_exit(void)
533 platform_device_unregister(sch311x_wdt_pdev);
534 platform_driver_unregister(&sch311x_wdt_driver);
537 module_init(sch311x_wdt_init);
538 module_exit(sch311x_wdt_exit);
540 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
541 MODULE_DESCRIPTION("SMSC SCH311x WatchDog Timer Driver");
542 MODULE_LICENSE("GPL");