blob: 7a4dfb95d08770f8e244ce01ce6c6e7fc4863df0 [file] [log] [blame]
Calin A. Culianueed65652006-01-14 13:20:46 -08001/*
2 * SBC EPX C3 0.1 A Hardware Watchdog Device for the Winsystems EPX-C3
3 * single board computer
4 *
5 * (c) Copyright 2006 Calin A. Culianu <calin@ajvar.org>, All Rights
6 * Reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * based on softdog.c by Alan Cox <alan@redhat.com>
14 */
15
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/config.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/fs.h>
22#include <linux/mm.h>
23#include <linux/miscdevice.h>
24#include <linux/watchdog.h>
25#include <linux/notifier.h>
26#include <linux/reboot.h>
27#include <linux/init.h>
Alan Coxde6c6422006-02-03 03:03:58 -080028#include <linux/ioport.h>
Calin A. Culianueed65652006-01-14 13:20:46 -080029#include <asm/uaccess.h>
30#include <asm/io.h>
31
32#define PFX "epx_c3: "
33static int epx_c3_alive;
34
35#define WATCHDOG_TIMEOUT 1 /* 1 sec default timeout */
36
37static int nowayout = WATCHDOG_NOWAYOUT;
38module_param(nowayout, int, 0);
39MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
40
41#define EPXC3_WATCHDOG_CTL_REG 0x1ee /* write 1 to enable, 0 to disable */
42#define EPXC3_WATCHDOG_PET_REG 0x1ef /* write anything to pet once enabled */
43
44static void epx_c3_start(void)
45{
46 outb(1, EPXC3_WATCHDOG_CTL_REG);
47}
48
49static void epx_c3_stop(void)
50{
51
52 outb(0, EPXC3_WATCHDOG_CTL_REG);
53
54 printk(KERN_INFO PFX "Stopped watchdog timer.\n");
55}
56
57static void epx_c3_pet(void)
58{
59 outb(1, EPXC3_WATCHDOG_PET_REG);
60}
61
62/*
63 * Allow only one person to hold it open
64 */
65static int epx_c3_open(struct inode *inode, struct file *file)
66{
67 if (epx_c3_alive)
68 return -EBUSY;
69
70 if (nowayout)
71 __module_get(THIS_MODULE);
72
73 /* Activate timer */
74 epx_c3_start();
75 epx_c3_pet();
76
77 epx_c3_alive = 1;
78 printk(KERN_INFO "Started watchdog timer.\n");
79
80 return nonseekable_open(inode, file);
81}
82
83static int epx_c3_release(struct inode *inode, struct file *file)
84{
85 /* Shut off the timer.
86 * Lock it in if it's a module and we defined ...NOWAYOUT */
87 if (!nowayout)
88 epx_c3_stop(); /* Turn the WDT off */
89
90 epx_c3_alive = 0;
91
92 return 0;
93}
94
95static ssize_t epx_c3_write(struct file *file, const char *data,
96 size_t len, loff_t *ppos)
97{
98 /* Refresh the timer. */
99 if (len)
100 epx_c3_pet();
101 return len;
102}
103
104static int epx_c3_ioctl(struct inode *inode, struct file *file,
105 unsigned int cmd, unsigned long arg)
106{
107 int options, retval = -EINVAL;
108 static struct watchdog_info ident = {
109 .options = WDIOF_KEEPALIVEPING |
110 WDIOF_MAGICCLOSE,
111 .firmware_version = 0,
112 .identity = "Winsystems EPX-C3 H/W Watchdog",
113 };
114
115 switch (cmd) {
116 case WDIOC_GETSUPPORT:
117 if (copy_to_user((struct watchdog_info *)arg,
118 &ident, sizeof(ident)))
119 return -EFAULT;
120 return 0;
121 case WDIOC_GETSTATUS:
122 case WDIOC_GETBOOTSTATUS:
123 return put_user(0,(int *)arg);
124 case WDIOC_KEEPALIVE:
125 epx_c3_pet();
126 return 0;
127 case WDIOC_GETTIMEOUT:
128 return put_user(WATCHDOG_TIMEOUT,(int *)arg);
129 case WDIOC_SETOPTIONS: {
130 if (get_user(options, (int *)arg))
131 return -EFAULT;
132
133 if (options & WDIOS_DISABLECARD) {
134 epx_c3_stop();
135 retval = 0;
136 }
137
138 if (options & WDIOS_ENABLECARD) {
139 epx_c3_start();
140 retval = 0;
141 }
142
143 return retval;
144 }
145 default:
146 return -ENOIOCTLCMD;
147 }
148}
149
150static int epx_c3_notify_sys(struct notifier_block *this, unsigned long code,
151 void *unused)
152{
153 if (code == SYS_DOWN || code == SYS_HALT)
154 epx_c3_stop(); /* Turn the WDT off */
155
156 return NOTIFY_DONE;
157}
158
159static struct file_operations epx_c3_fops = {
160 .owner = THIS_MODULE,
161 .llseek = no_llseek,
162 .write = epx_c3_write,
163 .ioctl = epx_c3_ioctl,
164 .open = epx_c3_open,
165 .release = epx_c3_release,
166};
167
168static struct miscdevice epx_c3_miscdev = {
169 .minor = WATCHDOG_MINOR,
170 .name = "watchdog",
171 .fops = &epx_c3_fops,
172};
173
174static struct notifier_block epx_c3_notifier = {
175 .notifier_call = epx_c3_notify_sys,
176};
177
178static const char banner[] __initdata =
179 KERN_INFO PFX "Hardware Watchdog Timer for Winsystems EPX-C3 SBC: 0.1\n";
180
181static int __init watchdog_init(void)
182{
183 int ret;
184
Alan Coxde6c6422006-02-03 03:03:58 -0800185 if (!request_region(EPXC3_WATCHDOG_CTL_REG, 2, "epxc3_watchdog"))
186 return -EBUSY;
187
Calin A. Culianueed65652006-01-14 13:20:46 -0800188 ret = register_reboot_notifier(&epx_c3_notifier);
189 if (ret) {
190 printk(KERN_ERR PFX "cannot register reboot notifier "
191 "(err=%d)\n", ret);
Alan Coxde6c6422006-02-03 03:03:58 -0800192 goto out;
Calin A. Culianueed65652006-01-14 13:20:46 -0800193 }
194
195 ret = misc_register(&epx_c3_miscdev);
196 if (ret) {
197 printk(KERN_ERR PFX "cannot register miscdev on minor=%d "
198 "(err=%d)\n", WATCHDOG_MINOR, ret);
199 unregister_reboot_notifier(&epx_c3_notifier);
Alan Coxde6c6422006-02-03 03:03:58 -0800200 goto out;
Calin A. Culianueed65652006-01-14 13:20:46 -0800201 }
202
203 printk(banner);
204
205 return 0;
Alan Coxde6c6422006-02-03 03:03:58 -0800206
207out:
208 release_region(EPXC3_WATCHDOG_CTL_REG, 2);
209 return ret;
Calin A. Culianueed65652006-01-14 13:20:46 -0800210}
211
212static void __exit watchdog_exit(void)
213{
214 misc_deregister(&epx_c3_miscdev);
215 unregister_reboot_notifier(&epx_c3_notifier);
Alan Coxde6c6422006-02-03 03:03:58 -0800216 release_region(EPXC3_WATCHDOG_CTL_REG, 2);
Calin A. Culianueed65652006-01-14 13:20:46 -0800217}
218
219module_init(watchdog_init);
220module_exit(watchdog_exit);
221
222MODULE_AUTHOR("Calin A. Culianu <calin@ajvar.org>");
223MODULE_DESCRIPTION("Hardware Watchdog Device for Winsystems EPX-C3 SBC. Note that there is no way to probe for this device -- so only use it if you are *sure* you are runnning on this specific SBC system from Winsystems! It writes to IO ports 0x1ee and 0x1ef!");
224MODULE_LICENSE("GPL");
225MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);