blob: 97f8a48d8b7870efbae1b9173d58295f65e2dd71 [file] [log] [blame]
James Chapman3be10212005-08-17 09:01:33 +02001/*
2 * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface
3 *
4 * Author: James Chapman <jchapman@katalix.com>
5 *
6 * Platform-specific setup code should configure the dog to generate
7 * interrupt or reset as required. This code only enables/disables
8 * and services the watchdog.
9 *
10 * Derived from mpc8xx_wdt.c, with the following copyright.
Alan Coxa86b8492008-05-19 14:07:26 +010011 *
James Chapman3be10212005-08-17 09:01:33 +020012 * 2002 (c) Florian Schirmer <jolt@tuxbox.org> This file is licensed under
13 * the terms of the GNU General Public License version 2. This program
14 * is licensed "as is" without any warranty of any kind, whether express
15 * or implied.
16 */
17
James Chapman3be10212005-08-17 09:01:33 +020018#include <linux/fs.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/miscdevice.h>
22#include <linux/module.h>
23#include <linux/watchdog.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
Dale Farnsworth7e07a152007-07-24 11:12:24 -070025#include <linux/mv643xx.h>
Alan Coxa86b8492008-05-19 14:07:26 +010026#include <linux/uaccess.h>
27#include <linux/io.h>
James Chapman3be10212005-08-17 09:01:33 +020028
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070029#define MV64x60_WDT_WDC_OFFSET 0
30
Dale Farnsworthf1869992007-07-24 11:31:25 -070031/*
32 * The watchdog configuration register contains a pair of 2-bit fields,
33 * 1. a reload field, bits 27-26, which triggers a reload of
34 * the countdown register, and
35 * 2. an enable field, bits 25-24, which toggles between
36 * enabling and disabling the watchdog timer.
37 * Bit 31 is a read-only field which indicates whether the
38 * watchdog timer is currently enabled.
39 *
40 * The low 24 bits contain the timer reload value.
41 */
42#define MV64x60_WDC_ENABLE_SHIFT 24
43#define MV64x60_WDC_SERVICE_SHIFT 26
44#define MV64x60_WDC_ENABLED_SHIFT 31
45
46#define MV64x60_WDC_ENABLED_TRUE 1
47#define MV64x60_WDC_ENABLED_FALSE 0
James Chapman3be10212005-08-17 09:01:33 +020048
49/* Flags bits */
50#define MV64x60_WDOG_FLAG_OPENED 0
James Chapman3be10212005-08-17 09:01:33 +020051
52static unsigned long wdt_flags;
53static int wdt_status;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070054static void __iomem *mv64x60_wdt_regs;
James Chapman3be10212005-08-17 09:01:33 +020055static int mv64x60_wdt_timeout;
Dale Farnsworthf1869992007-07-24 11:31:25 -070056static int mv64x60_wdt_count;
Dale Farnsworth94796f92007-07-24 11:15:26 -070057static unsigned int bus_clk;
Dale Farnsworthbf2fc922007-07-24 11:18:14 -070058static char expect_close;
Dale Farnsworthf1869992007-07-24 11:31:25 -070059static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
James Chapman3be10212005-08-17 09:01:33 +020060
Dale Farnsworthd37a5c32007-07-24 11:17:23 -070061static int nowayout = WATCHDOG_NOWAYOUT;
62module_param(nowayout, int, 0);
Alan Coxa86b8492008-05-19 14:07:26 +010063MODULE_PARM_DESC(nowayout,
64 "Watchdog cannot be stopped once started (default="
65 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Dale Farnsworthd37a5c32007-07-24 11:17:23 -070066
Dale Farnsworthf1869992007-07-24 11:31:25 -070067static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
James Chapman3be10212005-08-17 09:01:33 +020068{
Dale Farnsworthf1869992007-07-24 11:31:25 -070069 u32 data;
70 u32 enabled;
71 int ret = 0;
72
73 spin_lock(&mv64x60_wdt_spinlock);
74 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
75 enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
76
77 /* only toggle the requested field if enabled state matches predicate */
78 if ((enabled ^ enabled_predicate) == 0) {
79 /* We write a 1, then a 2 -- to the appropriate field */
80 data = (1 << field_shift) | mv64x60_wdt_count;
81 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
82
83 data = (2 << field_shift) | mv64x60_wdt_count;
84 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
85 ret = 1;
86 }
87 spin_unlock(&mv64x60_wdt_spinlock);
88
89 return ret;
James Chapman3be10212005-08-17 09:01:33 +020090}
91
92static void mv64x60_wdt_service(void)
93{
Dale Farnsworthf1869992007-07-24 11:31:25 -070094 mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
95 MV64x60_WDC_SERVICE_SHIFT);
James Chapman3be10212005-08-17 09:01:33 +020096}
97
98static void mv64x60_wdt_handler_enable(void)
99{
Dale Farnsworthf1869992007-07-24 11:31:25 -0700100 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
101 MV64x60_WDC_ENABLE_SHIFT)) {
102 mv64x60_wdt_service();
James Chapman3be10212005-08-17 09:01:33 +0200103 printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
104 }
105}
106
Dale Farnsworthf1869992007-07-24 11:31:25 -0700107static void mv64x60_wdt_handler_disable(void)
108{
109 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
110 MV64x60_WDC_ENABLE_SHIFT))
111 printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
112}
113
114static void mv64x60_wdt_set_timeout(unsigned int timeout)
Dale Farnsworth94796f92007-07-24 11:15:26 -0700115{
116 /* maximum bus cycle count is 0xFFFFFFFF */
117 if (timeout > 0xFFFFFFFF / bus_clk)
118 timeout = 0xFFFFFFFF / bus_clk;
119
Dale Farnsworthf1869992007-07-24 11:31:25 -0700120 mv64x60_wdt_count = timeout * bus_clk >> 8;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700121 mv64x60_wdt_timeout = timeout;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700122}
123
James Chapman3be10212005-08-17 09:01:33 +0200124static int mv64x60_wdt_open(struct inode *inode, struct file *file)
125{
126 if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
127 return -EBUSY;
128
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700129 if (nowayout)
130 __module_get(THIS_MODULE);
131
James Chapman3be10212005-08-17 09:01:33 +0200132 mv64x60_wdt_handler_enable();
133
Dale Farnsworth861e513772007-07-24 11:13:26 -0700134 return nonseekable_open(inode, file);
James Chapman3be10212005-08-17 09:01:33 +0200135}
136
137static int mv64x60_wdt_release(struct inode *inode, struct file *file)
138{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700139 if (expect_close == 42)
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700140 mv64x60_wdt_handler_disable();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700141 else {
142 printk(KERN_CRIT
143 "mv64x60_wdt: unexpected close, not stopping timer!\n");
144 mv64x60_wdt_service();
145 }
146 expect_close = 0;
James Chapman3be10212005-08-17 09:01:33 +0200147
148 clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
149
150 return 0;
151}
152
Al Virob2846df2005-09-29 00:42:27 +0100153static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
Alan Coxa86b8492008-05-19 14:07:26 +0100154 size_t len, loff_t *ppos)
James Chapman3be10212005-08-17 09:01:33 +0200155{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700156 if (len) {
157 if (!nowayout) {
158 size_t i;
159
160 expect_close = 0;
161
162 for (i = 0; i != len; i++) {
163 char c;
Alan Coxa86b8492008-05-19 14:07:26 +0100164 if (get_user(c, data + i))
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700165 return -EFAULT;
166 if (c == 'V')
167 expect_close = 42;
168 }
169 }
James Chapman3be10212005-08-17 09:01:33 +0200170 mv64x60_wdt_service();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700171 }
James Chapman3be10212005-08-17 09:01:33 +0200172
173 return len;
174}
175
Alan Coxa86b8492008-05-19 14:07:26 +0100176static long mv64x60_wdt_ioctl(struct file *file,
177 unsigned int cmd, unsigned long arg)
James Chapman3be10212005-08-17 09:01:33 +0200178{
Dale Farnsworth94796f92007-07-24 11:15:26 -0700179 int timeout;
Dale Farnsworth85d57232007-07-24 11:16:29 -0700180 int options;
Al Virob2846df2005-09-29 00:42:27 +0100181 void __user *argp = (void __user *)arg;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000182 static const struct watchdog_info info = {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700183 .options = WDIOF_SETTIMEOUT |
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700184 WDIOF_MAGICCLOSE |
Dale Farnsworth94796f92007-07-24 11:15:26 -0700185 WDIOF_KEEPALIVEPING,
James Chapman3be10212005-08-17 09:01:33 +0200186 .firmware_version = 0,
187 .identity = "MV64x60 watchdog",
188 };
189
190 switch (cmd) {
191 case WDIOC_GETSUPPORT:
Al Virob2846df2005-09-29 00:42:27 +0100192 if (copy_to_user(argp, &info, sizeof(info)))
James Chapman3be10212005-08-17 09:01:33 +0200193 return -EFAULT;
194 break;
195
196 case WDIOC_GETSTATUS:
197 case WDIOC_GETBOOTSTATUS:
Al Virob2846df2005-09-29 00:42:27 +0100198 if (put_user(wdt_status, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200199 return -EFAULT;
200 wdt_status &= ~WDIOF_KEEPALIVEPING;
201 break;
202
203 case WDIOC_GETTEMP:
204 return -EOPNOTSUPP;
205
206 case WDIOC_SETOPTIONS:
Dale Farnsworth85d57232007-07-24 11:16:29 -0700207 if (get_user(options, (int __user *)argp))
208 return -EFAULT;
209
210 if (options & WDIOS_DISABLECARD)
211 mv64x60_wdt_handler_disable();
212
213 if (options & WDIOS_ENABLECARD)
214 mv64x60_wdt_handler_enable();
215 break;
James Chapman3be10212005-08-17 09:01:33 +0200216
217 case WDIOC_KEEPALIVE:
218 mv64x60_wdt_service();
219 wdt_status |= WDIOF_KEEPALIVEPING;
220 break;
221
222 case WDIOC_SETTIMEOUT:
Dale Farnsworth94796f92007-07-24 11:15:26 -0700223 if (get_user(timeout, (int __user *)argp))
224 return -EFAULT;
225 mv64x60_wdt_set_timeout(timeout);
226 /* Fall through */
James Chapman3be10212005-08-17 09:01:33 +0200227
228 case WDIOC_GETTIMEOUT:
Dale Farnsworth264f0992007-07-24 11:14:21 -0700229 if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200230 return -EFAULT;
231 break;
232
233 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200234 return -ENOTTY;
James Chapman3be10212005-08-17 09:01:33 +0200235 }
236
237 return 0;
238}
239
Arjan van de Ven62322d22006-07-03 00:24:21 -0700240static const struct file_operations mv64x60_wdt_fops = {
James Chapman3be10212005-08-17 09:01:33 +0200241 .owner = THIS_MODULE,
242 .llseek = no_llseek,
243 .write = mv64x60_wdt_write,
Alan Coxa86b8492008-05-19 14:07:26 +0100244 .unlocked_ioctl = mv64x60_wdt_ioctl,
James Chapman3be10212005-08-17 09:01:33 +0200245 .open = mv64x60_wdt_open,
246 .release = mv64x60_wdt_release,
247};
248
249static struct miscdevice mv64x60_wdt_miscdev = {
250 .minor = WATCHDOG_MINOR,
251 .name = "watchdog",
252 .fops = &mv64x60_wdt_fops,
253};
254
Russell King3ae5eae2005-11-09 22:32:44 +0000255static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200256{
Russell King3ae5eae2005-11-09 22:32:44 +0000257 struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700258 struct resource *r;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700259 int timeout = 10;
James Chapman3be10212005-08-17 09:01:33 +0200260
Dale Farnsworth94796f92007-07-24 11:15:26 -0700261 bus_clk = 133; /* in MHz */
James Chapman3be10212005-08-17 09:01:33 +0200262 if (pdata) {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700263 timeout = pdata->timeout;
James Chapman3be10212005-08-17 09:01:33 +0200264 bus_clk = pdata->bus_clk;
265 }
266
Dale Farnsworth94796f92007-07-24 11:15:26 -0700267 /* Since bus_clk is truncated MHz, actual frequency could be
268 * up to 1MHz higher. Round up, since it's better to time out
269 * too late than too soon.
270 */
271 bus_clk++;
272 bus_clk *= 1000000; /* convert to Hz */
273
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700274 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
275 if (!r)
276 return -ENODEV;
277
H Hartley Sweetenb782a562009-12-04 12:24:04 -0500278 mv64x60_wdt_regs = ioremap(r->start, resource_size(r));
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700279 if (mv64x60_wdt_regs == NULL)
280 return -ENOMEM;
James Chapman3be10212005-08-17 09:01:33 +0200281
Dale Farnsworth94796f92007-07-24 11:15:26 -0700282 mv64x60_wdt_set_timeout(timeout);
James Chapman3be10212005-08-17 09:01:33 +0200283
Dale Farnsworth2422df52007-07-24 11:19:47 -0700284 mv64x60_wdt_handler_disable(); /* in case timer was already running */
285
James Chapman3be10212005-08-17 09:01:33 +0200286 return misc_register(&mv64x60_wdt_miscdev);
287}
288
Russell King3ae5eae2005-11-09 22:32:44 +0000289static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200290{
291 misc_deregister(&mv64x60_wdt_miscdev);
292
James Chapman3be10212005-08-17 09:01:33 +0200293 mv64x60_wdt_handler_disable();
294
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700295 iounmap(mv64x60_wdt_regs);
296
James Chapman3be10212005-08-17 09:01:33 +0200297 return 0;
298}
299
Russell King3ae5eae2005-11-09 22:32:44 +0000300static struct platform_driver mv64x60_wdt_driver = {
James Chapman3be10212005-08-17 09:01:33 +0200301 .probe = mv64x60_wdt_probe,
302 .remove = __devexit_p(mv64x60_wdt_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000303 .driver = {
304 .owner = THIS_MODULE,
305 .name = MV64x60_WDT_NAME,
306 },
James Chapman3be10212005-08-17 09:01:33 +0200307};
308
James Chapman3be10212005-08-17 09:01:33 +0200309static int __init mv64x60_wdt_init(void)
310{
James Chapman3be10212005-08-17 09:01:33 +0200311 printk(KERN_INFO "MV64x60 watchdog driver\n");
312
Dale Farnsworth422db8d2007-07-24 11:07:38 -0700313 return platform_driver_register(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200314}
315
316static void __exit mv64x60_wdt_exit(void)
317{
Russell King3ae5eae2005-11-09 22:32:44 +0000318 platform_driver_unregister(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200319}
320
321module_init(mv64x60_wdt_init);
322module_exit(mv64x60_wdt_exit);
323
324MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
325MODULE_DESCRIPTION("MV64x60 watchdog driver");
326MODULE_LICENSE("GPL");
327MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700328MODULE_ALIAS("platform:" MV64x60_WDT_NAME);