]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/watchdog/ep93xx_wdt.c
watchdog: ep93xx: Convert the watchdog driver into a platform device.
[mirror_ubuntu-hirsute-kernel.git] / drivers / watchdog / ep93xx_wdt.c
CommitLineData
f52ac8fe
AZ
1/*
2 * Watchdog driver for Cirrus Logic EP93xx family of devices.
3 *
4 * Copyright (c) 2004 Ray Lehtiniemi
5 * Copyright (c) 2006 Tower Technologies
6 * Based on ep93xx driver, bits from alim7101_wdt.c
7 *
8 * Authors: Ray Lehtiniemi <rayl@mail.com>,
9 * Alessandro Zummo <a.zummo@towertech.it>
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 *
15 * This watchdog fires after 250msec, which is a too short interval
16 * for us to rely on the user space daemon alone. So we ping the
17 * wdt each ~200msec and eventually stop doing it if the user space
18 * daemon dies.
19 *
20 * TODO:
21 *
22 * - Test last reset from watchdog status
23 * - Add a few missing ioctls
24 */
25
27c766aa
JP
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
3e0113a8 28#include <linux/platform_device.h>
f52ac8fe
AZ
29#include <linux/module.h>
30#include <linux/fs.h>
31#include <linux/miscdevice.h>
32#include <linux/watchdog.h>
33#include <linux/timer.h>
f339e2ac 34#include <linux/uaccess.h>
2653d1d7 35#include <linux/io.h>
f52ac8fe
AZ
36
37#define WDT_VERSION "0.3"
f52ac8fe
AZ
38
39/* default timeout (secs) */
40#define WDT_TIMEOUT 30
41
86a1e189 42static bool nowayout = WATCHDOG_NOWAYOUT;
f52ac8fe
AZ
43static int timeout = WDT_TIMEOUT;
44
3e0113a8 45static void __iomem *mmio_base;
f52ac8fe
AZ
46static struct timer_list timer;
47static unsigned long next_heartbeat;
48static unsigned long wdt_status;
49static unsigned long boot_status;
50
51#define WDT_IN_USE 0
52#define WDT_OK_TO_CLOSE 1
53
3e0113a8
HS
54#define EP93XX_WATCHDOG 0x00
55#define EP93XX_WDSTATUS 0x04
f52ac8fe
AZ
56
57/* reset the wdt every ~200ms */
58#define WDT_INTERVAL (HZ/5)
59
60static void wdt_enable(void)
61{
3e0113a8 62 writel(0xaaaa, mmio_base + EP93XX_WATCHDOG);
f52ac8fe
AZ
63}
64
65static void wdt_disable(void)
66{
3e0113a8 67 writel(0xaa55, mmio_base + EP93XX_WATCHDOG);
f52ac8fe
AZ
68}
69
70static inline void wdt_ping(void)
71{
3e0113a8 72 writel(0x5555, mmio_base + EP93XX_WATCHDOG);
f52ac8fe
AZ
73}
74
75static void wdt_startup(void)
76{
77 next_heartbeat = jiffies + (timeout * HZ);
78
79 wdt_enable();
80 mod_timer(&timer, jiffies + WDT_INTERVAL);
81}
82
83static void wdt_shutdown(void)
84{
85 del_timer_sync(&timer);
86 wdt_disable();
87}
88
89static void wdt_keepalive(void)
90{
91 /* user land ping */
92 next_heartbeat = jiffies + (timeout * HZ);
93}
94
95static int ep93xx_wdt_open(struct inode *inode, struct file *file)
96{
97 if (test_and_set_bit(WDT_IN_USE, &wdt_status))
98 return -EBUSY;
99
100 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
101
102 wdt_startup();
103
104 return nonseekable_open(inode, file);
105}
106
107static ssize_t
108ep93xx_wdt_write(struct file *file, const char __user *data, size_t len,
109 loff_t *ppos)
110{
f52ac8fe
AZ
111 if (len) {
112 if (!nowayout) {
113 size_t i;
114
115 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
116
117 for (i = 0; i != len; i++) {
118 char c;
119
120 if (get_user(c, data + i))
121 return -EFAULT;
122
123 if (c == 'V')
124 set_bit(WDT_OK_TO_CLOSE, &wdt_status);
125 else
126 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
127 }
128 }
129 wdt_keepalive();
130 }
131
132 return len;
133}
134
42747d71 135static const struct watchdog_info ident = {
f52ac8fe
AZ
136 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE,
137 .identity = "EP93xx Watchdog",
138};
139
f339e2ac
AC
140static long ep93xx_wdt_ioctl(struct file *file,
141 unsigned int cmd, unsigned long arg)
f52ac8fe 142{
795b89d2 143 int ret = -ENOTTY;
f52ac8fe
AZ
144
145 switch (cmd) {
146 case WDIOC_GETSUPPORT:
147 ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
148 sizeof(ident)) ? -EFAULT : 0;
149 break;
150
151 case WDIOC_GETSTATUS:
152 ret = put_user(0, (int __user *)arg);
153 break;
154
155 case WDIOC_GETBOOTSTATUS:
156 ret = put_user(boot_status, (int __user *)arg);
157 break;
158
f52ac8fe
AZ
159 case WDIOC_KEEPALIVE:
160 wdt_keepalive();
161 ret = 0;
162 break;
0c06090c
WVS
163
164 case WDIOC_GETTIMEOUT:
165 /* actually, it is 0.250 seconds.... */
166 ret = put_user(1, (int __user *)arg);
167 break;
f52ac8fe
AZ
168 }
169 return ret;
170}
171
172static int ep93xx_wdt_release(struct inode *inode, struct file *file)
173{
174 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
175 wdt_shutdown();
176 else
27c766aa 177 pr_crit("Device closed unexpectedly - timer will not stop\n");
f52ac8fe
AZ
178
179 clear_bit(WDT_IN_USE, &wdt_status);
180 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
181
182 return 0;
183}
184
62322d25 185static const struct file_operations ep93xx_wdt_fops = {
f52ac8fe
AZ
186 .owner = THIS_MODULE,
187 .write = ep93xx_wdt_write,
f339e2ac 188 .unlocked_ioctl = ep93xx_wdt_ioctl,
f52ac8fe
AZ
189 .open = ep93xx_wdt_open,
190 .release = ep93xx_wdt_release,
6038f373 191 .llseek = no_llseek,
f52ac8fe
AZ
192};
193
194static struct miscdevice ep93xx_wdt_miscdev = {
195 .minor = WATCHDOG_MINOR,
196 .name = "watchdog",
197 .fops = &ep93xx_wdt_fops,
198};
199
200static void ep93xx_timer_ping(unsigned long data)
201{
202 if (time_before(jiffies, next_heartbeat))
203 wdt_ping();
204
205 /* Re-set the timer interval */
206 mod_timer(&timer, jiffies + WDT_INTERVAL);
207}
208
3e0113a8 209static int __devinit ep93xx_wdt_probe(struct platform_device *pdev)
f52ac8fe 210{
3e0113a8
HS
211 struct resource *res;
212 unsigned long val;
f52ac8fe
AZ
213 int err;
214
3e0113a8
HS
215 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
216 if (!res)
217 return -ENXIO;
218
219 if (!devm_request_mem_region(&pdev->dev, res->start,
220 resource_size(res), pdev->name))
221 return -EBUSY;
222
223 mmio_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
224 if (!mmio_base)
225 return -ENXIO;
226
f52ac8fe
AZ
227 err = misc_register(&ep93xx_wdt_miscdev);
228
3e0113a8
HS
229 val = readl(mmio_base + EP93XX_WATCHDOG);
230 boot_status = val & 0x01 ? 1 : 0;
f52ac8fe 231
27c766aa 232 pr_info("EP93XX watchdog, driver version " WDT_VERSION "%s\n",
3e0113a8 233 (val & 0x08) ? " (nCS1 disable detected)" : "");
f52ac8fe
AZ
234
235 if (timeout < 1 || timeout > 3600) {
236 timeout = WDT_TIMEOUT;
27c766aa 237 pr_info("timeout value must be 1<=x<=3600, using %d\n",
f52ac8fe
AZ
238 timeout);
239 }
240
241 setup_timer(&timer, ep93xx_timer_ping, 1);
242 return err;
243}
244
3e0113a8 245static int __devexit ep93xx_wdt_remove(struct platform_device *pdev)
f52ac8fe
AZ
246{
247 wdt_shutdown();
248 misc_deregister(&ep93xx_wdt_miscdev);
3e0113a8 249 return 0;
f52ac8fe
AZ
250}
251
3e0113a8
HS
252static struct platform_driver ep93xx_wdt_driver = {
253 .driver = {
254 .owner = THIS_MODULE,
255 .name = "ep93xx-wdt",
256 },
257 .probe = ep93xx_wdt_probe,
258 .remove = __devexit_p(ep93xx_wdt_remove),
259};
260
261module_platform_driver(ep93xx_wdt_driver);
f52ac8fe 262
86a1e189 263module_param(nowayout, bool, 0);
f52ac8fe
AZ
264MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
265
266module_param(timeout, int, 0);
f339e2ac
AC
267MODULE_PARM_DESC(timeout,
268 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
d557f651 269 __MODULE_STRING(WDT_TIMEOUT) ")");
f52ac8fe
AZ
270
271MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>,"
272 "Alessandro Zummo <a.zummo@towertech.it>");
273MODULE_DESCRIPTION("EP93xx Watchdog");
274MODULE_LICENSE("GPL");
275MODULE_VERSION(WDT_VERSION);
276MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);