Linux Audio

Check our new training course

Yocto distribution development and maintenance

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