blob: 8ab757d3df5fba9c8867c3901298a19b76faa465 [file] [log] [blame]
Gilles Giganc4c28332007-10-31 16:31:42 +10001/*
2 * NANO7240 SBC Watchdog device driver
3 *
4 * Based on w83877f.c by Scott Jennings,
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation;
9 *
10 * Software distributed under the License is distributed on an "AS IS"
11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or
12 * implied. See the License for the specific language governing
13 * rights and limitations under the License.
14 *
15 * (c) Copyright 2007 Gilles GIGAN <gilles.gigan@jcu.edu.au>
16 *
17 */
18
Joe Perches27c766a2012-02-15 15:06:19 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Gilles Giganc4c28332007-10-31 16:31:42 +100021#include <linux/fs.h>
22#include <linux/init.h>
23#include <linux/ioport.h>
24#include <linux/jiffies.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/miscdevice.h>
28#include <linux/notifier.h>
29#include <linux/reboot.h>
30#include <linux/types.h>
31#include <linux/watchdog.h>
Alan Cox619a8a22008-05-19 14:08:16 +010032#include <linux/io.h>
33#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070034#include <linux/atomic.h>
Gilles Giganc4c28332007-10-31 16:31:42 +100035#include <asm/system.h>
Gilles Giganc4c28332007-10-31 16:31:42 +100036
Gilles Giganc4c28332007-10-31 16:31:42 +100037#define SBC7240_ENABLE_PORT 0x443
38#define SBC7240_DISABLE_PORT 0x043
39#define SBC7240_SET_TIMEOUT_PORT SBC7240_ENABLE_PORT
40#define SBC7240_MAGIC_CHAR 'V'
41
42#define SBC7240_TIMEOUT 30
43#define SBC7240_MAX_TIMEOUT 255
44static int timeout = SBC7240_TIMEOUT; /* in seconds */
45module_param(timeout, int, 0);
46MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<="
47 __MODULE_STRING(SBC7240_MAX_TIMEOUT) ", default="
48 __MODULE_STRING(SBC7240_TIMEOUT) ")");
49
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010050static bool nowayout = WATCHDOG_NOWAYOUT;
51module_param(nowayout, bool, 0);
Gilles Giganc4c28332007-10-31 16:31:42 +100052MODULE_PARM_DESC(nowayout, "Disable watchdog when closing device file");
53
54#define SBC7240_OPEN_STATUS_BIT 0
55#define SBC7240_ENABLED_STATUS_BIT 1
56#define SBC7240_EXPECT_CLOSE_STATUS_BIT 2
57static unsigned long wdt_status;
58
59/*
60 * Utility routines
61 */
62
63static void wdt_disable(void)
64{
65 /* disable the watchdog */
66 if (test_and_clear_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
67 inb_p(SBC7240_DISABLE_PORT);
Joe Perches27c766a2012-02-15 15:06:19 -080068 pr_info("Watchdog timer is now disabled\n");
Gilles Giganc4c28332007-10-31 16:31:42 +100069 }
70}
71
72static void wdt_enable(void)
73{
74 /* enable the watchdog */
75 if (!test_and_set_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status)) {
76 inb_p(SBC7240_ENABLE_PORT);
Joe Perches27c766a2012-02-15 15:06:19 -080077 pr_info("Watchdog timer is now enabled\n");
Gilles Giganc4c28332007-10-31 16:31:42 +100078 }
79}
80
81static int wdt_set_timeout(int t)
82{
83 if (t < 1 || t > SBC7240_MAX_TIMEOUT) {
Joe Perches27c766a2012-02-15 15:06:19 -080084 pr_err("timeout value must be 1<=x<=%d\n", SBC7240_MAX_TIMEOUT);
Gilles Giganc4c28332007-10-31 16:31:42 +100085 return -1;
86 }
87 /* set the timeout */
88 outb_p((unsigned)t, SBC7240_SET_TIMEOUT_PORT);
89 timeout = t;
Joe Perches27c766a2012-02-15 15:06:19 -080090 pr_info("timeout set to %d seconds\n", t);
Gilles Giganc4c28332007-10-31 16:31:42 +100091 return 0;
92}
93
94/* Whack the dog */
95static inline void wdt_keepalive(void)
96{
97 if (test_bit(SBC7240_ENABLED_STATUS_BIT, &wdt_status))
98 inb_p(SBC7240_ENABLE_PORT);
99}
100
101/*
102 * /dev/watchdog handling
103 */
104static ssize_t fop_write(struct file *file, const char __user *buf,
105 size_t count, loff_t *ppos)
106{
107 size_t i;
108 char c;
109
110 if (count) {
111 if (!nowayout) {
112 clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
113 &wdt_status);
114
115 /* is there a magic char ? */
116 for (i = 0; i != count; i++) {
117 if (get_user(c, buf + i))
118 return -EFAULT;
119 if (c == SBC7240_MAGIC_CHAR) {
120 set_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT,
121 &wdt_status);
122 break;
123 }
124 }
125 }
126
127 wdt_keepalive();
128 }
129
130 return count;
131}
132
133static int fop_open(struct inode *inode, struct file *file)
134{
135 if (test_and_set_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status))
136 return -EBUSY;
137
138 wdt_enable();
139
140 return nonseekable_open(inode, file);
141}
142
143static int fop_close(struct inode *inode, struct file *file)
144{
145 if (test_and_clear_bit(SBC7240_EXPECT_CLOSE_STATUS_BIT, &wdt_status)
146 || !nowayout) {
147 wdt_disable();
148 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800149 pr_crit("Unexpected close, not stopping watchdog!\n");
Gilles Giganc4c28332007-10-31 16:31:42 +1000150 wdt_keepalive();
151 }
152
153 clear_bit(SBC7240_OPEN_STATUS_BIT, &wdt_status);
154 return 0;
155}
156
Alan Cox619a8a22008-05-19 14:08:16 +0100157static const struct watchdog_info ident = {
Gilles Giganc4c28332007-10-31 16:31:42 +1000158 .options = WDIOF_KEEPALIVEPING|
159 WDIOF_SETTIMEOUT|
160 WDIOF_MAGICCLOSE,
161 .firmware_version = 1,
162 .identity = "SBC7240",
163};
164
165
Alan Cox619a8a22008-05-19 14:08:16 +0100166static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Gilles Giganc4c28332007-10-31 16:31:42 +1000167{
168 switch (cmd) {
169 case WDIOC_GETSUPPORT:
Alan Cox619a8a22008-05-19 14:08:16 +0100170 return copy_to_user((void __user *)arg, &ident, sizeof(ident))
171 ? -EFAULT : 0;
Gilles Giganc4c28332007-10-31 16:31:42 +1000172 case WDIOC_GETSTATUS:
173 case WDIOC_GETBOOTSTATUS:
174 return put_user(0, (int __user *)arg);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000175 case WDIOC_SETOPTIONS:
176 {
177 int options;
178 int retval = -EINVAL;
179
180 if (get_user(options, (int __user *)arg))
181 return -EFAULT;
182
183 if (options & WDIOS_DISABLECARD) {
184 wdt_disable();
185 retval = 0;
186 }
187
188 if (options & WDIOS_ENABLECARD) {
189 wdt_enable();
190 retval = 0;
191 }
192
193 return retval;
194 }
Gilles Giganc4c28332007-10-31 16:31:42 +1000195 case WDIOC_KEEPALIVE:
196 wdt_keepalive();
197 return 0;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000198 case WDIOC_SETTIMEOUT:
199 {
200 int new_timeout;
Gilles Giganc4c28332007-10-31 16:31:42 +1000201
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000202 if (get_user(new_timeout, (int __user *)arg))
203 return -EFAULT;
Gilles Giganc4c28332007-10-31 16:31:42 +1000204
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000205 if (wdt_set_timeout(new_timeout))
206 return -EINVAL;
Gilles Giganc4c28332007-10-31 16:31:42 +1000207
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000208 /* Fall through */
209 }
Gilles Giganc4c28332007-10-31 16:31:42 +1000210 case WDIOC_GETTIMEOUT:
211 return put_user(timeout, (int __user *)arg);
212 default:
213 return -ENOTTY;
214 }
215}
216
217static const struct file_operations wdt_fops = {
218 .owner = THIS_MODULE,
219 .llseek = no_llseek,
220 .write = fop_write,
221 .open = fop_open,
222 .release = fop_close,
Alan Cox619a8a22008-05-19 14:08:16 +0100223 .unlocked_ioctl = fop_ioctl,
Gilles Giganc4c28332007-10-31 16:31:42 +1000224};
225
226static struct miscdevice wdt_miscdev = {
227 .minor = WATCHDOG_MINOR,
228 .name = "watchdog",
229 .fops = &wdt_fops,
230};
231
232/*
233 * Notifier for system down
234 */
235
236static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
237 void *unused)
238{
239 if (code == SYS_DOWN || code == SYS_HALT)
240 wdt_disable();
241 return NOTIFY_DONE;
242}
243
244static struct notifier_block wdt_notifier = {
245 .notifier_call = wdt_notify_sys,
246};
247
248static void __exit sbc7240_wdt_unload(void)
249{
Joe Perches27c766a2012-02-15 15:06:19 -0800250 pr_info("Removing watchdog\n");
Gilles Giganc4c28332007-10-31 16:31:42 +1000251 misc_deregister(&wdt_miscdev);
252
253 unregister_reboot_notifier(&wdt_notifier);
254 release_region(SBC7240_ENABLE_PORT, 1);
255}
256
257static int __init sbc7240_wdt_init(void)
258{
259 int rc = -EBUSY;
260
261 if (!request_region(SBC7240_ENABLE_PORT, 1, "SBC7240 WDT")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800262 pr_err("I/O address 0x%04x already in use\n",
Gilles Giganc4c28332007-10-31 16:31:42 +1000263 SBC7240_ENABLE_PORT);
264 rc = -EIO;
265 goto err_out;
266 }
267
268 /* The IO port 0x043 used to disable the watchdog
269 * is already claimed by the system timer, so we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300270 * can't request_region() it ...*/
Gilles Giganc4c28332007-10-31 16:31:42 +1000271
272 if (timeout < 1 || timeout > SBC7240_MAX_TIMEOUT) {
273 timeout = SBC7240_TIMEOUT;
Joe Perches27c766a2012-02-15 15:06:19 -0800274 pr_info("timeout value must be 1<=x<=%d, using %d\n",
275 SBC7240_MAX_TIMEOUT, timeout);
Gilles Giganc4c28332007-10-31 16:31:42 +1000276 }
277 wdt_set_timeout(timeout);
278 wdt_disable();
279
280 rc = register_reboot_notifier(&wdt_notifier);
281 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800282 pr_err("cannot register reboot notifier (err=%d)\n", rc);
Gilles Giganc4c28332007-10-31 16:31:42 +1000283 goto err_out_region;
284 }
285
286 rc = misc_register(&wdt_miscdev);
287 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800288 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
Gilles Giganc4c28332007-10-31 16:31:42 +1000289 wdt_miscdev.minor, rc);
290 goto err_out_reboot_notifier;
291 }
292
Joe Perches27c766a2012-02-15 15:06:19 -0800293 pr_info("Watchdog driver for SBC7240 initialised (nowayout=%d)\n",
294 nowayout);
Gilles Giganc4c28332007-10-31 16:31:42 +1000295
296 return 0;
297
298err_out_reboot_notifier:
299 unregister_reboot_notifier(&wdt_notifier);
300err_out_region:
301 release_region(SBC7240_ENABLE_PORT, 1);
302err_out:
303 return rc;
304}
305
306module_init(sbc7240_wdt_init);
307module_exit(sbc7240_wdt_unload);
308
309MODULE_AUTHOR("Gilles Gigan");
310MODULE_DESCRIPTION("Watchdog device driver for single board"
311 " computers EPIC Nano 7240 from iEi");
312MODULE_LICENSE("GPL");
313MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
314