blob: f8477002b728bcfe1b84a3874be93f0765504cd3 [file] [log] [blame]
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +00001/*
2 * sch311x_wdt.c - Driver for the SCH311x Super-I/O chips
3 * integrated watchdog.
4 *
5 * (c) Copyright 2008 Wim Van Sebroeck <wim@iguana.be>.
6 *
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.
11 *
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.
15 */
16
17/*
18 * Includes, defines, variables, module parameters, ...
19 */
20
Joe Perches27c766a2012-02-15 15:06:19 -080021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +000023/* Includes */
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 MODULE_ALIAS_MISCDEV
30 (WATCHDOG_MINOR) */
31#include <linux/watchdog.h> /* For the watchdog specific items */
32#include <linux/init.h> /* For __init/__exit/... */
33#include <linux/fs.h> /* For file operations */
34#include <linux/platform_device.h> /* For platform_driver framework */
35#include <linux/ioport.h> /* For io-port access */
36#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
37#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
38#include <linux/io.h> /* For inb/outb/... */
39
40/* Module and version information */
41#define DRV_NAME "sch311x_wdt"
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +000042
43/* Runtime registers */
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +000044#define GP60 0x47
45#define WDT_TIME_OUT 0x65
46#define WDT_VAL 0x66
47#define WDT_CFG 0x67
48#define WDT_CTRL 0x68
49
50/* internal variables */
51static unsigned long sch311x_wdt_is_open;
52static char sch311x_wdt_expect_close;
53static struct platform_device *sch311x_wdt_pdev;
54
55static int sch311x_ioports[] = { 0x2e, 0x4e, 0x162e, 0x164e, 0x00 };
56
57static struct { /* The devices private data */
58 /* the Runtime Register base address */
59 unsigned short runtime_reg;
60 /* The card's boot status */
61 int boot_status;
62 /* the lock for io operations */
63 spinlock_t io_lock;
64} sch311x_wdt_data;
65
66/* Module load parameters */
67static unsigned short force_id;
68module_param(force_id, ushort, 0);
69MODULE_PARM_DESC(force_id, "Override the detected device ID");
70
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +000071#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
72static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
73module_param(timeout, int, 0);
74MODULE_PARM_DESC(timeout,
75 "Watchdog timeout in seconds. 1<= timeout <=15300, default="
76 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
77
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010078static bool nowayout = WATCHDOG_NOWAYOUT;
79module_param(nowayout, bool, 0);
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +000080MODULE_PARM_DESC(nowayout,
81 "Watchdog cannot be stopped once started (default="
82 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
83
84/*
85 * Super-IO functions
86 */
87
88static inline void sch311x_sio_enter(int sio_config_port)
89{
90 outb(0x55, sio_config_port);
91}
92
93static inline void sch311x_sio_exit(int sio_config_port)
94{
95 outb(0xaa, sio_config_port);
96}
97
98static inline int sch311x_sio_inb(int sio_config_port, int reg)
99{
100 outb(reg, sio_config_port);
101 return inb(sio_config_port + 1);
102}
103
104static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
105{
106 outb(reg, sio_config_port);
107 outb(val, sio_config_port + 1);
108}
109
110/*
111 * Watchdog Operations
112 */
113
114static void sch311x_wdt_set_timeout(int t)
115{
116 unsigned char timeout_unit = 0x80;
117
118 /* When new timeout is bigger then 255 seconds, we will use minutes */
119 if (t > 255) {
120 timeout_unit = 0;
121 t /= 60;
122 }
123
124 /* -- Watchdog Timeout --
125 * Bit 0-6 (Reserved)
126 * Bit 7 WDT Time-out Value Units Select
127 * (0 = Minutes, 1 = Seconds)
128 */
129 outb(timeout_unit, sch311x_wdt_data.runtime_reg + WDT_TIME_OUT);
130
131 /* -- Watchdog Timer Time-out Value --
132 * Bit 0-7 Binary coded units (0=Disabled, 1..255)
133 */
134 outb(t, sch311x_wdt_data.runtime_reg + WDT_VAL);
135}
136
137static void sch311x_wdt_start(void)
138{
139 spin_lock(&sch311x_wdt_data.io_lock);
140
141 /* set watchdog's timeout */
142 sch311x_wdt_set_timeout(timeout);
143 /* enable the watchdog */
144 /* -- General Purpose I/O Bit 6.0 --
145 * Bit 0, In/Out: 0 = Output, 1 = Input
146 * Bit 1, Polarity: 0 = No Invert, 1 = Invert
147 * Bit 2-3, Function select: 00 = GPI/O, 01 = LED1, 11 = WDT,
148 * 10 = Either Edge Triggered Intr.4
149 * Bit 4-6 (Reserved)
150 * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
151 */
152 outb(0x0e, sch311x_wdt_data.runtime_reg + GP60);
153
154 spin_unlock(&sch311x_wdt_data.io_lock);
155
156}
157
158static void sch311x_wdt_stop(void)
159{
160 spin_lock(&sch311x_wdt_data.io_lock);
161
162 /* stop the watchdog */
163 outb(0x01, sch311x_wdt_data.runtime_reg + GP60);
164 /* disable timeout by setting it to 0 */
165 sch311x_wdt_set_timeout(0);
166
167 spin_unlock(&sch311x_wdt_data.io_lock);
168}
169
170static void sch311x_wdt_keepalive(void)
171{
172 spin_lock(&sch311x_wdt_data.io_lock);
173 sch311x_wdt_set_timeout(timeout);
174 spin_unlock(&sch311x_wdt_data.io_lock);
175}
176
177static int sch311x_wdt_set_heartbeat(int t)
178{
179 if (t < 1 || t > (255*60))
180 return -EINVAL;
181
182 /* When new timeout is bigger then 255 seconds,
183 * we will round up to minutes (with a max of 255) */
184 if (t > 255)
185 t = (((t - 1) / 60) + 1) * 60;
186
187 timeout = t;
188 return 0;
189}
190
191static void sch311x_wdt_get_status(int *status)
192{
193 unsigned char new_status;
194
195 *status = 0;
196
197 spin_lock(&sch311x_wdt_data.io_lock);
198
199 /* -- Watchdog timer control --
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300200 * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000201 * Bit 1 Reserved
202 * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
203 * Bit 3 P20 Force Timeout enabled:
204 * 0 = P20 activity does not generate the WD timeout event
205 * 1 = P20 Allows rising edge of P20, from the keyboard
206 * controller, to force the WD timeout event.
207 * Bit 4-7 Reserved
208 */
209 new_status = inb(sch311x_wdt_data.runtime_reg + WDT_CTRL);
210 if (new_status & 0x01)
211 *status |= WDIOF_CARDRESET;
212
213 spin_unlock(&sch311x_wdt_data.io_lock);
214}
215
216/*
217 * /dev/watchdog handling
218 */
219
220static ssize_t sch311x_wdt_write(struct file *file, const char __user *buf,
221 size_t count, loff_t *ppos)
222{
223 if (count) {
224 if (!nowayout) {
225 size_t i;
226
227 sch311x_wdt_expect_close = 0;
228
229 for (i = 0; i != count; i++) {
230 char c;
231 if (get_user(c, buf + i))
232 return -EFAULT;
233 if (c == 'V')
234 sch311x_wdt_expect_close = 42;
235 }
236 }
237 sch311x_wdt_keepalive();
238 }
239 return count;
240}
241
242static long sch311x_wdt_ioctl(struct file *file, unsigned int cmd,
243 unsigned long arg)
244{
245 int status;
246 int new_timeout;
247 void __user *argp = (void __user *)arg;
248 int __user *p = argp;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000249 static const struct watchdog_info ident = {
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000250 .options = WDIOF_KEEPALIVEPING |
251 WDIOF_SETTIMEOUT |
252 WDIOF_MAGICCLOSE,
253 .firmware_version = 1,
254 .identity = DRV_NAME,
255 };
256
257 switch (cmd) {
258 case WDIOC_GETSUPPORT:
259 if (copy_to_user(argp, &ident, sizeof(ident)))
260 return -EFAULT;
261 break;
262
263 case WDIOC_GETSTATUS:
264 {
265 sch311x_wdt_get_status(&status);
266 return put_user(status, p);
267 }
268 case WDIOC_GETBOOTSTATUS:
269 return put_user(sch311x_wdt_data.boot_status, p);
270
271 case WDIOC_SETOPTIONS:
272 {
273 int options, retval = -EINVAL;
274
275 if (get_user(options, p))
276 return -EFAULT;
277 if (options & WDIOS_DISABLECARD) {
278 sch311x_wdt_stop();
279 retval = 0;
280 }
281 if (options & WDIOS_ENABLECARD) {
282 sch311x_wdt_start();
283 retval = 0;
284 }
285 return retval;
286 }
287 case WDIOC_KEEPALIVE:
288 sch311x_wdt_keepalive();
289 break;
290
291 case WDIOC_SETTIMEOUT:
292 if (get_user(new_timeout, p))
293 return -EFAULT;
294 if (sch311x_wdt_set_heartbeat(new_timeout))
295 return -EINVAL;
296 sch311x_wdt_keepalive();
297 /* Fall */
298 case WDIOC_GETTIMEOUT:
299 return put_user(timeout, p);
300 default:
301 return -ENOTTY;
302 }
303 return 0;
304}
305
306static int sch311x_wdt_open(struct inode *inode, struct file *file)
307{
308 if (test_and_set_bit(0, &sch311x_wdt_is_open))
309 return -EBUSY;
310 /*
311 * Activate
312 */
313 sch311x_wdt_start();
314 return nonseekable_open(inode, file);
315}
316
317static int sch311x_wdt_close(struct inode *inode, struct file *file)
318{
319 if (sch311x_wdt_expect_close == 42) {
320 sch311x_wdt_stop();
321 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800322 pr_crit("Unexpected close, not stopping watchdog!\n");
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000323 sch311x_wdt_keepalive();
324 }
325 clear_bit(0, &sch311x_wdt_is_open);
326 sch311x_wdt_expect_close = 0;
327 return 0;
328}
329
330/*
331 * Kernel Interfaces
332 */
333
334static const struct file_operations sch311x_wdt_fops = {
335 .owner = THIS_MODULE,
336 .llseek = no_llseek,
337 .write = sch311x_wdt_write,
338 .unlocked_ioctl = sch311x_wdt_ioctl,
339 .open = sch311x_wdt_open,
340 .release = sch311x_wdt_close,
341};
342
343static struct miscdevice sch311x_wdt_miscdev = {
344 .minor = WATCHDOG_MINOR,
345 .name = "watchdog",
346 .fops = &sch311x_wdt_fops,
347};
348
349/*
350 * Init & exit routines
351 */
352
353static int __devinit sch311x_wdt_probe(struct platform_device *pdev)
354{
355 struct device *dev = &pdev->dev;
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000356 int err;
357
358 spin_lock_init(&sch311x_wdt_data.io_lock);
359
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000360 if (!request_region(sch311x_wdt_data.runtime_reg + GP60, 1, DRV_NAME)) {
361 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
362 sch311x_wdt_data.runtime_reg + GP60,
363 sch311x_wdt_data.runtime_reg + GP60);
364 err = -EBUSY;
Dave Mueller8f90a3a2012-04-11 15:43:22 +0200365 goto exit;
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000366 }
367
368 if (!request_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4,
369 DRV_NAME)) {
370 dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
371 sch311x_wdt_data.runtime_reg + WDT_TIME_OUT,
372 sch311x_wdt_data.runtime_reg + WDT_CTRL);
373 err = -EBUSY;
Dave Mueller8f90a3a2012-04-11 15:43:22 +0200374 goto exit_release_region;
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000375 }
376
377 /* Make sure that the watchdog is not running */
378 sch311x_wdt_stop();
379
380 /* Disable keyboard and mouse interaction and interrupt */
381 /* -- Watchdog timer configuration --
382 * Bit 0 Reserved
383 * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
384 * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
385 * Bit 3 Reserved
386 * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
387 * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
388 */
389 outb(0, sch311x_wdt_data.runtime_reg + WDT_CFG);
390
391 /* Check that the heartbeat value is within it's range ;
392 * if not reset to the default */
393 if (sch311x_wdt_set_heartbeat(timeout)) {
394 sch311x_wdt_set_heartbeat(WATCHDOG_TIMEOUT);
395 dev_info(dev, "timeout value must be 1<=x<=15300, using %d\n",
396 timeout);
397 }
398
399 /* Get status at boot */
400 sch311x_wdt_get_status(&sch311x_wdt_data.boot_status);
401
Wim Van Sebroeck4c4b6382010-06-10 06:46:56 +0000402 sch311x_wdt_miscdev.parent = dev;
403
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000404 err = misc_register(&sch311x_wdt_miscdev);
405 if (err != 0) {
406 dev_err(dev, "cannot register miscdev on minor=%d (err=%d)\n",
407 WATCHDOG_MINOR, err);
Dave Mueller8f90a3a2012-04-11 15:43:22 +0200408 goto exit_release_region2;
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000409 }
410
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000411 dev_info(dev,
412 "SMSC SCH311x WDT initialized. timeout=%d sec (nowayout=%d)\n",
413 timeout, nowayout);
414
415 return 0;
416
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000417exit_release_region2:
Dave Mueller8f90a3a2012-04-11 15:43:22 +0200418 release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000419exit_release_region:
Dave Mueller8f90a3a2012-04-11 15:43:22 +0200420 release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000421 sch311x_wdt_data.runtime_reg = 0;
422exit:
423 return err;
424}
425
426static int __devexit sch311x_wdt_remove(struct platform_device *pdev)
427{
428 /* Stop the timer before we leave */
429 if (!nowayout)
430 sch311x_wdt_stop();
431
432 /* Deregister */
433 misc_deregister(&sch311x_wdt_miscdev);
434 release_region(sch311x_wdt_data.runtime_reg + WDT_TIME_OUT, 4);
435 release_region(sch311x_wdt_data.runtime_reg + GP60, 1);
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000436 sch311x_wdt_data.runtime_reg = 0;
437 return 0;
438}
439
440static void sch311x_wdt_shutdown(struct platform_device *dev)
441{
442 /* Turn the WDT off if we have a soft shutdown */
443 sch311x_wdt_stop();
444}
445
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000446static struct platform_driver sch311x_wdt_driver = {
447 .probe = sch311x_wdt_probe,
448 .remove = __devexit_p(sch311x_wdt_remove),
449 .shutdown = sch311x_wdt_shutdown,
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000450 .driver = {
451 .owner = THIS_MODULE,
452 .name = DRV_NAME,
453 },
454};
455
456static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
457{
458 int err = 0, reg;
459 unsigned short base_addr;
460 unsigned char dev_id;
461
462 sch311x_sio_enter(sio_config_port);
463
464 /* Check device ID. We currently know about:
465 * SCH3112 (0x7c), SCH3114 (0x7d), and SCH3116 (0x7f). */
466 reg = force_id ? force_id : sch311x_sio_inb(sio_config_port, 0x20);
467 if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
468 err = -ENODEV;
469 goto exit;
470 }
471 dev_id = reg == 0x7c ? 2 : reg == 0x7d ? 4 : 6;
472
473 /* Select logical device A (runtime registers) */
474 sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
475
476 /* Check if Logical Device Register is currently active */
Dan Carpenter6899a8e2011-02-23 23:26:01 +0300477 if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800478 pr_info("Seems that LDN 0x0a is not active...\n");
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000479
480 /* Get the base address of the runtime registers */
481 base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
482 sch311x_sio_inb(sio_config_port, 0x61);
483 if (!base_addr) {
Joe Perches27c766a2012-02-15 15:06:19 -0800484 pr_err("Base address not set\n");
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000485 err = -ENODEV;
486 goto exit;
487 }
488 *addr = base_addr;
489
Joe Perches27c766a2012-02-15 15:06:19 -0800490 pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id, base_addr);
Wim Van Sebroeck4c6e63b2008-10-22 08:59:25 +0000491
492exit:
493 sch311x_sio_exit(sio_config_port);
494 return err;
495}
496
497static int __init sch311x_wdt_init(void)
498{
499 int err, i, found = 0;
500 unsigned short addr = 0;
501
502 for (i = 0; !found && sch311x_ioports[i]; i++)
503 if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
504 found++;
505
506 if (!found)
507 return -ENODEV;
508
509 sch311x_wdt_data.runtime_reg = addr;
510
511 err = platform_driver_register(&sch311x_wdt_driver);
512 if (err)
513 return err;
514
515 sch311x_wdt_pdev = platform_device_register_simple(DRV_NAME, addr,
516 NULL, 0);
517
518 if (IS_ERR(sch311x_wdt_pdev)) {
519 err = PTR_ERR(sch311x_wdt_pdev);
520 goto unreg_platform_driver;
521 }
522
523 return 0;
524
525unreg_platform_driver:
526 platform_driver_unregister(&sch311x_wdt_driver);
527 return err;
528}
529
530static void __exit sch311x_wdt_exit(void)
531{
532 platform_device_unregister(sch311x_wdt_pdev);
533 platform_driver_unregister(&sch311x_wdt_driver);
534}
535
536module_init(sch311x_wdt_init);
537module_exit(sch311x_wdt_exit);
538
539MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
540MODULE_DESCRIPTION("SMSC SCH311x WatchDog Timer Driver");
541MODULE_LICENSE("GPL");
542MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
543