1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
/*
* sch311x_wdt.c - Driver for the SCH311x Super-I/O chips
* integrated watchdog.
*
* (c) Copyright 2008 Wim Van Sebroeck <wim@iguana.be>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
* provide warranty for any of this software. This material is
* provided "AS-IS" and at no charge.
*/
/*
* Includes, defines, variables, module parameters, ...
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
/* Includes */
#include <linux/module.h> /* For module specific items */
#include <linux/moduleparam.h> /* For new moduleparam's */
#include <linux/types.h> /* For standard types (like size_t) */
#include <linux/errno.h> /* For the -ENODEV/... values */
#include <linux/kernel.h> /* For printk/... */
#include <linux/miscdevice.h> /* For struct miscdevice */
#include <linux/watchdog.h> /* For the watchdog specific items */
#include <linux/init.h> /* For __init/__exit/... */
#include <linux/fs.h> /* For file operations */
#include <linux/platform_device.h> /* For platform_driver framework */
#include <linux/ioport.h> /* For io-port access */
#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
#include <linux/io.h> /* For inb/outb/... */
/* Module and version information */
#define DRV_NAME "sch311x_wdt"
/* Runtime registers */
#define GP60 0x47
#define WDT_TIME_OUT 0x65
#define WDT_VAL 0x66
#define WDT_CFG 0x67
#define WDT_CTRL 0x68
/* internal variables */
static unsigned long sch311x_wdt_is_open;
static char sch311x_wdt_expect_close;
static struct platform_device *sch311x_wdt_pdev;
static int sch311x_ioports[] = { 0x2e, 0x4e, 0x162e, 0x164e, 0x00 };
static struct { /* The devices private data */
/* the Runtime Register base address */
unsigned short runtime_reg;
/* The card's boot status */
int boot_status;
/* the lock for io operations */
spinlock_t io_lock;
} sch311x_wdt_data;
/* Module load parameters */
static unsigned short force_id;
module_param(force_id, ushort, 0);
MODULE_PARM_DESC(force_id, "Override the detected device ID");
#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
module_param(timeout, int, 0);
MODULE_PARM_DESC(timeout,
"Watchdog timeout in seconds. 1<= timeout <=15300, default="
__MODULE_STRING(WATCHDOG_TIMEOUT) ".");
static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
MODULE_PARM_DESC(nowayout,
"Watchdog cannot be stopped once started (default="
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
/*
* Super-IO functions
*/
static inline void sch311x_sio_enter(int sio_config_port)
{
outb(0x55, sio_config_port);
}
static inline void sch311x_sio_exit(int sio_config_port)
{
outb(0xaa, sio_config_port);
}
static inline int sch311x_sio_inb(int sio_config_port, int reg)
{
outb(reg, sio_config_port);
return inb(sio_config_port + 1);
}
static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
{
outb(reg, sio_config_port);
outb(val, sio_config_port + 1);
}
/*
* Watchdog Operations
*/
static void sch311x_wdt_set_timeout(int t)
{
unsigned char timeout_unit = 0x80;
/* When new timeout is bigger then 255 seconds, we will use minutes */
if (t > 255) {
timeout_unit = 0;
t /= 60;
}
/* -- Watchdog Timeout --
* Bit 0-6 (Reserved)
* Bit 7 WDT Time-out Value Units Select
* (0 = Minutes, 1 = Seconds)
*/
outb(timeout_unit, sch311x_wdt_data.runtime_reg + WDT_TIME_OUT);
/* -- Watchdog Timer Time-out Value --
* Bit 0-7 Binary coded units (0=Disabled, 1..255)
*/
outb(t, sch311x_wdt_data.runtime_reg + WDT_VAL);
}
static void sch311x_wdt_start(void)
{
unsigned char t;
spin_lock(&sch311x_wdt_data.io_lock);
/* set watchdog's timeout */
sch311x_wdt_set_timeout(timeout);
/* enable the watchdog */
/* -- General Purpose I/O Bit 6.0 --
* Bit 0, In/Out: 0 = Output, 1 = Input
* Bit 1, Polarity: 0 = No Invert, 1 = Invert
* Bit 2-3, Function select: 00 = GPI/O, 01 = LED1, 11 = WDT,
* 10 = Either Edge Triggered Intr.4
* Bit 4-6 (Reserved)
* Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
*/
t = inb(sch311x_wdt_data.runtime_reg + GP60);
outb((t & ~0x0d) | 0x0c, sch311x_wdt_data.runtime_reg + GP60);
spin_unlock(&sch311x_wdt_data.io_lock);
}
static void sch311x_wdt_stop(void)
{
unsigned char t;
spin_lock(&sch311x_wdt_data.io_lock);
/* stop the watchdog */
t = inb(sch311x_wdt_data.runtime_reg + GP60);
outb((t & ~0x0d) | 0x01, sch311x_wdt_data.runtime_reg + GP60);
/* disable timeout by setting it to 0 */
sch311x_wdt_set_timeout(0);
spin_unlock(&sch311x_wdt_data.io_lock);
}
static void sch311x_wdt_keepalive(void)
{
spin_lock(&sch311x_wdt_data.io_lock);
sch311x_wdt_set_timeout(timeout);
spin_unlock(&sch311x_wdt_data.io_lock);
}
static int sch311x_wdt_set_heartbeat(int t)
{
if (t < 1 || t > (255*60))
return -EINVAL;
/* When new timeout is bigger then 255 seconds,
* we will round up to minutes (with a max of 255) */
if (t > 255)
t = (((t - 1) / 60) + 1) * 60;
timeout = t;
return 0;
}
static void sch311x_wdt_get_status(int *status)
{
unsigned char new_status;
*status = 0;
spin_lock(&sch311x_wdt_data.io_lock);
/* -- Watchdog timer control --
* Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
* Bit 1 Reserved
* Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
* Bit 3 P20 Force Timeout enabled:
* 0 = P20 activity does not generate the WD timeout event
* 1 = P20 Allows rising edge of P20, from the keyboard
* controller, to force the WD timeout event.
* Bit 4-7 Reserved
*/
new_status = inb(sch311x_wdt_data.runtime_reg + WDT_CTRL);
if (new_status & 0x01)
*status |= WDIOF_CARDRESET;
spin_unlock(&sch311x_wdt_data.io_lock);
}
/*
* /dev/watchdog handling
*/
static ssize_t sch311x_wdt_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
if (count) {
if (!nowayout) {
size_t i;
sch311x_wdt_expect_close = 0;
for (i = 0; i != count; i++) {
char c;
if (get_user(c, buf + i))
return -EFAULT;
if (c == 'V')
sch311x_wdt_expect_close = 42;
}
}
sch311x_wdt_keepalive();
}
return count;
}
static long sch311x_wdt_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
int status;
int new_timeout;
void __user *argp = (void __user *)arg;
int __user *p = argp;
static const struct watchdog_info ident = {
.options = WDIOF_KEEPALIVEPING |
WDIOF_SETTIMEOUT |
WDIOF_MAGICCLOSE,
.firmware_version = 1,
.identity = DRV_NAME,
};
switch (cmd) {
case WDIOC_GETSUPPORT:
if (copy_to_user(argp, &ident, sizeof(ident)))
return -EFAULT;
break;
case WDIOC_GETSTATUS:
{
sch311x_wdt_get_status(&status);
return put_user(status, p);
}
case WDIOC_GETBOOTSTATUS:
return put_user(sch311x_wdt_data.boot_status, p);
case WDIOC_SETOPTIONS:
{
int options, retval = -EINVAL;
if (get_user(options, p))
return -EFAULT;
if (options & WDIOS_DISABLECARD) {
sch311x_wdt_stop();
retval = 0;
}
if (options & WDIOS_ENABLECARD) {
sch311x_wdt_start();
retval = 0;
}
return retval;
}
case WDIOC_KEEPALIVE:
sch311x_wdt_keepalive();
break;
case WDIOC_SETTIMEOUT:
if (get_user(new_timeout,<
|