blob: ca9c6c4360eeda0cd75746469e8f5a022642236e [file] [log] [blame]
Denis Turischev3a5f9002009-07-21 13:13:29 +03001/*
2 * Watchdog driver for SBC-FITPC2 board
3 *
4 * Author: Denis Turischev <denis@compulab.co.il>
5 *
6 * Adapted from the IXP2000 watchdog driver by Deepak Saxena.
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME " WATCHDOG: " fmt
14
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/miscdevice.h>
18#include <linux/watchdog.h>
19#include <linux/ioport.h>
20#include <linux/delay.h>
21#include <linux/fs.h>
22#include <linux/init.h>
23#include <linux/moduleparam.h>
24#include <linux/dmi.h>
25#include <linux/io.h>
26#include <linux/uaccess.h>
27
28#include <asm/system.h>
29
30static int nowayout = WATCHDOG_NOWAYOUT;
31static unsigned int margin = 60; /* (secs) Default is 1 minute */
32static unsigned long wdt_status;
Denis Turischev322af982010-04-22 19:54:20 +030033static DEFINE_MUTEX(wdt_lock);
Denis Turischev3a5f9002009-07-21 13:13:29 +030034
35#define WDT_IN_USE 0
36#define WDT_OK_TO_CLOSE 1
37
38#define COMMAND_PORT 0x4c
39#define DATA_PORT 0x48
40
41#define IFACE_ON_COMMAND 1
42#define REBOOT_COMMAND 2
43
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +000044#define WATCHDOG_NAME "SBC-FITPC2 Watchdog"
Denis Turischev3a5f9002009-07-21 13:13:29 +030045
46static void wdt_send_data(unsigned char command, unsigned char data)
47{
Denis Turischev3a5f9002009-07-21 13:13:29 +030048 outb(data, DATA_PORT);
Denis Turischevef39a1b2010-01-21 16:10:07 +020049 msleep(200);
Denis Turischevfcf1dd72010-04-22 19:50:03 +030050 outb(command, COMMAND_PORT);
51 msleep(100);
Denis Turischev3a5f9002009-07-21 13:13:29 +030052}
53
54static void wdt_enable(void)
55{
Denis Turischev322af982010-04-22 19:54:20 +030056 mutex_lock(&wdt_lock);
Denis Turischev3a5f9002009-07-21 13:13:29 +030057 wdt_send_data(IFACE_ON_COMMAND, 1);
58 wdt_send_data(REBOOT_COMMAND, margin);
Denis Turischev322af982010-04-22 19:54:20 +030059 mutex_unlock(&wdt_lock);
Denis Turischev3a5f9002009-07-21 13:13:29 +030060}
61
62static void wdt_disable(void)
63{
Denis Turischev322af982010-04-22 19:54:20 +030064 mutex_lock(&wdt_lock);
Denis Turischev3a5f9002009-07-21 13:13:29 +030065 wdt_send_data(IFACE_ON_COMMAND, 0);
66 wdt_send_data(REBOOT_COMMAND, 0);
Denis Turischev322af982010-04-22 19:54:20 +030067 mutex_unlock(&wdt_lock);
Denis Turischev3a5f9002009-07-21 13:13:29 +030068}
69
70static int fitpc2_wdt_open(struct inode *inode, struct file *file)
71{
72 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
73 return -EBUSY;
74
75 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
76
77 wdt_enable();
78
79 return nonseekable_open(inode, file);
80}
81
82static ssize_t fitpc2_wdt_write(struct file *file, const char *data,
83 size_t len, loff_t *ppos)
84{
85 size_t i;
86
87 if (!len)
88 return 0;
89
90 if (nowayout) {
91 len = 0;
92 goto out;
93 }
94
95 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
96
97 for (i = 0; i != len; i++) {
98 char c;
99
100 if (get_user(c, data + i))
101 return -EFAULT;
102
103 if (c == 'V')
104 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
105 }
106
107out:
108 wdt_enable();
109
110 return len;
111}
112
113
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000114static const struct watchdog_info ident = {
Denis Turischev3a5f9002009-07-21 13:13:29 +0300115 .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
116 WDIOF_KEEPALIVEPING,
117 .identity = WATCHDOG_NAME,
118};
119
120
121static long fitpc2_wdt_ioctl(struct file *file, unsigned int cmd,
122 unsigned long arg)
123{
124 int ret = -ENOTTY;
125 int time;
126
127 switch (cmd) {
128 case WDIOC_GETSUPPORT:
129 ret = copy_to_user((struct watchdog_info *)arg, &ident,
130 sizeof(ident)) ? -EFAULT : 0;
131 break;
132
133 case WDIOC_GETSTATUS:
134 ret = put_user(0, (int *)arg);
135 break;
136
137 case WDIOC_GETBOOTSTATUS:
138 ret = put_user(0, (int *)arg);
139 break;
140
141 case WDIOC_KEEPALIVE:
142 wdt_enable();
143 ret = 0;
144 break;
145
146 case WDIOC_SETTIMEOUT:
147 ret = get_user(time, (int *)arg);
148 if (ret)
149 break;
150
151 if (time < 31 || time > 255) {
152 ret = -EINVAL;
153 break;
154 }
155
156 margin = time;
157 wdt_enable();
158 /* Fall through */
159
160 case WDIOC_GETTIMEOUT:
161 ret = put_user(margin, (int *)arg);
162 break;
163 }
164
165 return ret;
166}
167
168static int fitpc2_wdt_release(struct inode *inode, struct file *file)
169{
170 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
171 wdt_disable();
172 pr_info("Device disabled\n");
173 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800174 pr_warn("Device closed unexpectedly - timer will not stop\n");
Denis Turischev3a5f9002009-07-21 13:13:29 +0300175 wdt_enable();
176 }
177
178 clear_bit(WDT_IN_USE, &wdt_status);
179 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
180
181 return 0;
182}
183
184
185static const struct file_operations fitpc2_wdt_fops = {
186 .owner = THIS_MODULE,
187 .llseek = no_llseek,
188 .write = fitpc2_wdt_write,
189 .unlocked_ioctl = fitpc2_wdt_ioctl,
190 .open = fitpc2_wdt_open,
191 .release = fitpc2_wdt_release,
192};
193
194static struct miscdevice fitpc2_wdt_miscdev = {
195 .minor = WATCHDOG_MINOR,
196 .name = "watchdog",
197 .fops = &fitpc2_wdt_fops,
198};
199
200static int __init fitpc2_wdt_init(void)
201{
202 int err;
Jiri Slabyd4065772011-02-28 10:16:29 +0100203 const char *brd_name;
Denis Turischev3a5f9002009-07-21 13:13:29 +0300204
Jiri Slabyd4065772011-02-28 10:16:29 +0100205 brd_name = dmi_get_system_info(DMI_BOARD_NAME);
206
207 if (!brd_name || !strstr(brd_name, "SBC-FITPC2"))
Denis Turischev3a5f9002009-07-21 13:13:29 +0300208 return -ENODEV;
Denis Turischevef39a1b2010-01-21 16:10:07 +0200209
Jiri Slabyd4065772011-02-28 10:16:29 +0100210 pr_info("%s found\n", brd_name);
Denis Turischev3a5f9002009-07-21 13:13:29 +0300211
212 if (!request_region(COMMAND_PORT, 1, WATCHDOG_NAME)) {
213 pr_err("I/O address 0x%04x already in use\n", COMMAND_PORT);
214 return -EIO;
215 }
216
217 if (!request_region(DATA_PORT, 1, WATCHDOG_NAME)) {
218 pr_err("I/O address 0x%04x already in use\n", DATA_PORT);
219 err = -EIO;
220 goto err_data_port;
221 }
222
223 if (margin < 31 || margin > 255) {
Joe Perches27c766a2012-02-15 15:06:19 -0800224 pr_err("margin must be in range 31 - 255 seconds, you tried to set %d\n",
225 margin);
Denis Turischev3a5f9002009-07-21 13:13:29 +0300226 err = -EINVAL;
227 goto err_margin;
228 }
229
230 err = misc_register(&fitpc2_wdt_miscdev);
Denis Turischev1efd3742009-11-05 13:32:40 +0200231 if (err) {
Denis Turischev3a5f9002009-07-21 13:13:29 +0300232 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
Joe Perches27c766a2012-02-15 15:06:19 -0800233 WATCHDOG_MINOR, err);
Denis Turischev3a5f9002009-07-21 13:13:29 +0300234 goto err_margin;
235 }
236
237 return 0;
238
239err_margin:
240 release_region(DATA_PORT, 1);
241err_data_port:
242 release_region(COMMAND_PORT, 1);
243
244 return err;
245}
246
247static void __exit fitpc2_wdt_exit(void)
248{
249 misc_deregister(&fitpc2_wdt_miscdev);
250 release_region(DATA_PORT, 1);
251 release_region(COMMAND_PORT, 1);
252}
253
254module_init(fitpc2_wdt_init);
255module_exit(fitpc2_wdt_exit);
256
257MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
258MODULE_DESCRIPTION("SBC-FITPC2 Watchdog");
259
260module_param(margin, int, 0);
261MODULE_PARM_DESC(margin, "Watchdog margin in seconds (default 60s)");
262
263module_param(nowayout, int, 0);
264MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
265
266MODULE_LICENSE("GPL");
267MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
268