blob: 35c49da30b421a84285b6a2fdb146089183227ab [file] [log] [blame]
Alessandro Zummof52ac8f2006-03-25 03:06:37 -08001/*
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
Joe Perches27c766a2012-02-15 15:06:19 -080026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110028#include <linux/platform_device.h>
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080029#include <linux/module.h>
30#include <linux/fs.h>
31#include <linux/miscdevice.h>
32#include <linux/watchdog.h>
33#include <linux/timer.h>
Alan Coxf339e2a2008-05-19 14:05:35 +010034#include <linux/uaccess.h>
Ryan Mallon2653d1d2009-07-15 21:33:22 +010035#include <linux/io.h>
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080036
37#define WDT_VERSION "0.3"
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080038
39/* default timeout (secs) */
40#define WDT_TIMEOUT 30
41
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010042static bool nowayout = WATCHDOG_NOWAYOUT;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080043static int timeout = WDT_TIMEOUT;
44
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110045static void __iomem *mmio_base;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080046static 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
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110054#define EP93XX_WATCHDOG 0x00
55#define EP93XX_WDSTATUS 0x04
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080056
57/* reset the wdt every ~200ms */
58#define WDT_INTERVAL (HZ/5)
59
60static void wdt_enable(void)
61{
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110062 writel(0xaaaa, mmio_base + EP93XX_WATCHDOG);
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080063}
64
65static void wdt_disable(void)
66{
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110067 writel(0xaa55, mmio_base + EP93XX_WATCHDOG);
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080068}
69
70static inline void wdt_ping(void)
71{
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +110072 writel(0x5555, mmio_base + EP93XX_WATCHDOG);
Alessandro Zummof52ac8f2006-03-25 03:06:37 -080073}
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{
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800111 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
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000135static const struct watchdog_info ident = {
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800136 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE,
137 .identity = "EP93xx Watchdog",
138};
139
Alan Coxf339e2a2008-05-19 14:05:35 +0100140static long ep93xx_wdt_ioctl(struct file *file,
141 unsigned int cmd, unsigned long arg)
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800142{
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200143 int ret = -ENOTTY;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800144
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
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800159 case WDIOC_KEEPALIVE:
160 wdt_keepalive();
161 ret = 0;
162 break;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000163
164 case WDIOC_GETTIMEOUT:
165 /* actually, it is 0.250 seconds.... */
166 ret = put_user(1, (int __user *)arg);
167 break;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800168 }
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
Joe Perches27c766a2012-02-15 15:06:19 -0800177 pr_crit("Device closed unexpectedly - timer will not stop\n");
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800178
179 clear_bit(WDT_IN_USE, &wdt_status);
180 clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
181
182 return 0;
183}
184
Arjan van de Ven62322d22006-07-03 00:24:21 -0700185static const struct file_operations ep93xx_wdt_fops = {
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800186 .owner = THIS_MODULE,
187 .write = ep93xx_wdt_write,
Alan Coxf339e2a2008-05-19 14:05:35 +0100188 .unlocked_ioctl = ep93xx_wdt_ioctl,
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800189 .open = ep93xx_wdt_open,
190 .release = ep93xx_wdt_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200191 .llseek = no_llseek,
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800192};
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
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100209static int __devinit ep93xx_wdt_probe(struct platform_device *pdev)
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800210{
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100211 struct resource *res;
212 unsigned long val;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800213 int err;
214
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100215 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
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800227 err = misc_register(&ep93xx_wdt_miscdev);
228
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100229 val = readl(mmio_base + EP93XX_WATCHDOG);
230 boot_status = val & 0x01 ? 1 : 0;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800231
Joe Perches27c766a2012-02-15 15:06:19 -0800232 pr_info("EP93XX watchdog, driver version " WDT_VERSION "%s\n",
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100233 (val & 0x08) ? " (nCS1 disable detected)" : "");
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800234
235 if (timeout < 1 || timeout > 3600) {
236 timeout = WDT_TIMEOUT;
Joe Perches27c766a2012-02-15 15:06:19 -0800237 pr_info("timeout value must be 1<=x<=3600, using %d\n",
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800238 timeout);
239 }
240
241 setup_timer(&timer, ep93xx_timer_ping, 1);
242 return err;
243}
244
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100245static int __devexit ep93xx_wdt_remove(struct platform_device *pdev)
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800246{
247 wdt_shutdown();
248 misc_deregister(&ep93xx_wdt_miscdev);
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100249 return 0;
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800250}
251
H Hartley Sweeten3e0113a2012-03-13 09:48:16 +1100252static 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);
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800262
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100263module_param(nowayout, bool, 0);
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800264MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
265
266module_param(timeout, int, 0);
Alan Coxf339e2a2008-05-19 14:05:35 +0100267MODULE_PARM_DESC(timeout,
268 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
Wim Van Sebroeckd557f652010-05-03 08:58:56 +0000269 __MODULE_STRING(WDT_TIMEOUT) ")");
Alessandro Zummof52ac8f2006-03-25 03:06:37 -0800270
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);