blob: 0365c317f7e1a960ea91c8e94cf8b24cd029ab69 [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.
11 *
12 * 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>
25
Dale Farnsworth7e07a152007-07-24 11:12:24 -070026#include <linux/mv643xx.h>
James Chapman3be10212005-08-17 09:01:33 +020027#include <asm/uaccess.h>
28#include <asm/io.h>
29
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070030#define MV64x60_WDT_WDC_OFFSET 0
31
Dale Farnsworthf1869992007-07-24 11:31:25 -070032/*
33 * The watchdog configuration register contains a pair of 2-bit fields,
34 * 1. a reload field, bits 27-26, which triggers a reload of
35 * the countdown register, and
36 * 2. an enable field, bits 25-24, which toggles between
37 * enabling and disabling the watchdog timer.
38 * Bit 31 is a read-only field which indicates whether the
39 * watchdog timer is currently enabled.
40 *
41 * The low 24 bits contain the timer reload value.
42 */
43#define MV64x60_WDC_ENABLE_SHIFT 24
44#define MV64x60_WDC_SERVICE_SHIFT 26
45#define MV64x60_WDC_ENABLED_SHIFT 31
46
47#define MV64x60_WDC_ENABLED_TRUE 1
48#define MV64x60_WDC_ENABLED_FALSE 0
James Chapman3be10212005-08-17 09:01:33 +020049
50/* Flags bits */
51#define MV64x60_WDOG_FLAG_OPENED 0
James Chapman3be10212005-08-17 09:01:33 +020052
53static unsigned long wdt_flags;
54static int wdt_status;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070055static void __iomem *mv64x60_wdt_regs;
James Chapman3be10212005-08-17 09:01:33 +020056static int mv64x60_wdt_timeout;
Dale Farnsworthf1869992007-07-24 11:31:25 -070057static int mv64x60_wdt_count;
Dale Farnsworth94796f92007-07-24 11:15:26 -070058static unsigned int bus_clk;
Dale Farnsworthbf2fc922007-07-24 11:18:14 -070059static char expect_close;
Dale Farnsworthf1869992007-07-24 11:31:25 -070060static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
James Chapman3be10212005-08-17 09:01:33 +020061
Dale Farnsworthd37a5c32007-07-24 11:17:23 -070062static int nowayout = WATCHDOG_NOWAYOUT;
63module_param(nowayout, int, 0);
64MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
65
Dale Farnsworthf1869992007-07-24 11:31:25 -070066static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
James Chapman3be10212005-08-17 09:01:33 +020067{
Dale Farnsworthf1869992007-07-24 11:31:25 -070068 u32 data;
69 u32 enabled;
70 int ret = 0;
71
72 spin_lock(&mv64x60_wdt_spinlock);
73 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
74 enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
75
76 /* only toggle the requested field if enabled state matches predicate */
77 if ((enabled ^ enabled_predicate) == 0) {
78 /* We write a 1, then a 2 -- to the appropriate field */
79 data = (1 << field_shift) | mv64x60_wdt_count;
80 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
81
82 data = (2 << field_shift) | mv64x60_wdt_count;
83 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
84 ret = 1;
85 }
86 spin_unlock(&mv64x60_wdt_spinlock);
87
88 return ret;
James Chapman3be10212005-08-17 09:01:33 +020089}
90
91static void mv64x60_wdt_service(void)
92{
Dale Farnsworthf1869992007-07-24 11:31:25 -070093 mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
94 MV64x60_WDC_SERVICE_SHIFT);
James Chapman3be10212005-08-17 09:01:33 +020095}
96
97static void mv64x60_wdt_handler_enable(void)
98{
Dale Farnsworthf1869992007-07-24 11:31:25 -070099 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
100 MV64x60_WDC_ENABLE_SHIFT)) {
101 mv64x60_wdt_service();
James Chapman3be10212005-08-17 09:01:33 +0200102 printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
103 }
104}
105
Dale Farnsworthf1869992007-07-24 11:31:25 -0700106static void mv64x60_wdt_handler_disable(void)
107{
108 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
109 MV64x60_WDC_ENABLE_SHIFT))
110 printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
111}
112
113static void mv64x60_wdt_set_timeout(unsigned int timeout)
Dale Farnsworth94796f92007-07-24 11:15:26 -0700114{
115 /* maximum bus cycle count is 0xFFFFFFFF */
116 if (timeout > 0xFFFFFFFF / bus_clk)
117 timeout = 0xFFFFFFFF / bus_clk;
118
Dale Farnsworthf1869992007-07-24 11:31:25 -0700119 mv64x60_wdt_count = timeout * bus_clk >> 8;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700120 mv64x60_wdt_timeout = timeout;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700121}
122
James Chapman3be10212005-08-17 09:01:33 +0200123static int mv64x60_wdt_open(struct inode *inode, struct file *file)
124{
125 if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
126 return -EBUSY;
127
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700128 if (nowayout)
129 __module_get(THIS_MODULE);
130
James Chapman3be10212005-08-17 09:01:33 +0200131 mv64x60_wdt_handler_enable();
132
Dale Farnsworth861e513772007-07-24 11:13:26 -0700133 return nonseekable_open(inode, file);
James Chapman3be10212005-08-17 09:01:33 +0200134}
135
136static int mv64x60_wdt_release(struct inode *inode, struct file *file)
137{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700138 if (expect_close == 42)
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700139 mv64x60_wdt_handler_disable();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700140 else {
141 printk(KERN_CRIT
142 "mv64x60_wdt: unexpected close, not stopping timer!\n");
143 mv64x60_wdt_service();
144 }
145 expect_close = 0;
James Chapman3be10212005-08-17 09:01:33 +0200146
147 clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
148
149 return 0;
150}
151
Al Virob2846df2005-09-29 00:42:27 +0100152static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
James Chapman3be10212005-08-17 09:01:33 +0200153 size_t len, loff_t * ppos)
154{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700155 if (len) {
156 if (!nowayout) {
157 size_t i;
158
159 expect_close = 0;
160
161 for (i = 0; i != len; i++) {
162 char c;
163 if(get_user(c, data + i))
164 return -EFAULT;
165 if (c == 'V')
166 expect_close = 42;
167 }
168 }
James Chapman3be10212005-08-17 09:01:33 +0200169 mv64x60_wdt_service();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700170 }
James Chapman3be10212005-08-17 09:01:33 +0200171
172 return len;
173}
174
175static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
176 unsigned int cmd, unsigned long arg)
177{
Dale Farnsworth94796f92007-07-24 11:15:26 -0700178 int timeout;
Dale Farnsworth85d57232007-07-24 11:16:29 -0700179 int options;
Al Virob2846df2005-09-29 00:42:27 +0100180 void __user *argp = (void __user *)arg;
James Chapman3be10212005-08-17 09:01:33 +0200181 static struct watchdog_info info = {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700182 .options = WDIOF_SETTIMEOUT |
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700183 WDIOF_MAGICCLOSE |
Dale Farnsworth94796f92007-07-24 11:15:26 -0700184 WDIOF_KEEPALIVEPING,
James Chapman3be10212005-08-17 09:01:33 +0200185 .firmware_version = 0,
186 .identity = "MV64x60 watchdog",
187 };
188
189 switch (cmd) {
190 case WDIOC_GETSUPPORT:
Al Virob2846df2005-09-29 00:42:27 +0100191 if (copy_to_user(argp, &info, sizeof(info)))
James Chapman3be10212005-08-17 09:01:33 +0200192 return -EFAULT;
193 break;
194
195 case WDIOC_GETSTATUS:
196 case WDIOC_GETBOOTSTATUS:
Al Virob2846df2005-09-29 00:42:27 +0100197 if (put_user(wdt_status, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200198 return -EFAULT;
199 wdt_status &= ~WDIOF_KEEPALIVEPING;
200 break;
201
202 case WDIOC_GETTEMP:
203 return -EOPNOTSUPP;
204
205 case WDIOC_SETOPTIONS:
Dale Farnsworth85d57232007-07-24 11:16:29 -0700206 if (get_user(options, (int __user *)argp))
207 return -EFAULT;
208
209 if (options & WDIOS_DISABLECARD)
210 mv64x60_wdt_handler_disable();
211
212 if (options & WDIOS_ENABLECARD)
213 mv64x60_wdt_handler_enable();
214 break;
James Chapman3be10212005-08-17 09:01:33 +0200215
216 case WDIOC_KEEPALIVE:
217 mv64x60_wdt_service();
218 wdt_status |= WDIOF_KEEPALIVEPING;
219 break;
220
221 case WDIOC_SETTIMEOUT:
Dale Farnsworth94796f92007-07-24 11:15:26 -0700222 if (get_user(timeout, (int __user *)argp))
223 return -EFAULT;
224 mv64x60_wdt_set_timeout(timeout);
225 /* Fall through */
James Chapman3be10212005-08-17 09:01:33 +0200226
227 case WDIOC_GETTIMEOUT:
Dale Farnsworth264f0992007-07-24 11:14:21 -0700228 if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200229 return -EFAULT;
230 break;
231
232 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200233 return -ENOTTY;
James Chapman3be10212005-08-17 09:01:33 +0200234 }
235
236 return 0;
237}
238
Arjan van de Ven62322d22006-07-03 00:24:21 -0700239static const struct file_operations mv64x60_wdt_fops = {
James Chapman3be10212005-08-17 09:01:33 +0200240 .owner = THIS_MODULE,
241 .llseek = no_llseek,
242 .write = mv64x60_wdt_write,
243 .ioctl = mv64x60_wdt_ioctl,
244 .open = mv64x60_wdt_open,
245 .release = mv64x60_wdt_release,
246};
247
248static struct miscdevice mv64x60_wdt_miscdev = {
249 .minor = WATCHDOG_MINOR,
250 .name = "watchdog",
251 .fops = &mv64x60_wdt_fops,
252};
253
Russell King3ae5eae2005-11-09 22:32:44 +0000254static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200255{
Russell King3ae5eae2005-11-09 22:32:44 +0000256 struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700257 struct resource *r;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700258 int timeout = 10;
James Chapman3be10212005-08-17 09:01:33 +0200259
Dale Farnsworth94796f92007-07-24 11:15:26 -0700260 bus_clk = 133; /* in MHz */
James Chapman3be10212005-08-17 09:01:33 +0200261 if (pdata) {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700262 timeout = pdata->timeout;
James Chapman3be10212005-08-17 09:01:33 +0200263 bus_clk = pdata->bus_clk;
264 }
265
Dale Farnsworth94796f92007-07-24 11:15:26 -0700266 /* Since bus_clk is truncated MHz, actual frequency could be
267 * up to 1MHz higher. Round up, since it's better to time out
268 * too late than too soon.
269 */
270 bus_clk++;
271 bus_clk *= 1000000; /* convert to Hz */
272
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700273 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
274 if (!r)
275 return -ENODEV;
276
277 mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1);
278 if (mv64x60_wdt_regs == NULL)
279 return -ENOMEM;
James Chapman3be10212005-08-17 09:01:33 +0200280
Dale Farnsworth94796f92007-07-24 11:15:26 -0700281 mv64x60_wdt_set_timeout(timeout);
James Chapman3be10212005-08-17 09:01:33 +0200282
Dale Farnsworth2422df52007-07-24 11:19:47 -0700283 mv64x60_wdt_handler_disable(); /* in case timer was already running */
284
James Chapman3be10212005-08-17 09:01:33 +0200285 return misc_register(&mv64x60_wdt_miscdev);
286}
287
Russell King3ae5eae2005-11-09 22:32:44 +0000288static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200289{
290 misc_deregister(&mv64x60_wdt_miscdev);
291
James Chapman3be10212005-08-17 09:01:33 +0200292 mv64x60_wdt_handler_disable();
293
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700294 iounmap(mv64x60_wdt_regs);
295
James Chapman3be10212005-08-17 09:01:33 +0200296 return 0;
297}
298
Russell King3ae5eae2005-11-09 22:32:44 +0000299static struct platform_driver mv64x60_wdt_driver = {
James Chapman3be10212005-08-17 09:01:33 +0200300 .probe = mv64x60_wdt_probe,
301 .remove = __devexit_p(mv64x60_wdt_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000302 .driver = {
303 .owner = THIS_MODULE,
304 .name = MV64x60_WDT_NAME,
305 },
James Chapman3be10212005-08-17 09:01:33 +0200306};
307
James Chapman3be10212005-08-17 09:01:33 +0200308static int __init mv64x60_wdt_init(void)
309{
James Chapman3be10212005-08-17 09:01:33 +0200310 printk(KERN_INFO "MV64x60 watchdog driver\n");
311
Dale Farnsworth422db8d2007-07-24 11:07:38 -0700312 return platform_driver_register(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200313}
314
315static void __exit mv64x60_wdt_exit(void)
316{
Russell King3ae5eae2005-11-09 22:32:44 +0000317 platform_driver_unregister(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200318}
319
320module_init(mv64x60_wdt_init);
321module_exit(mv64x60_wdt_exit);
322
323MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
324MODULE_DESCRIPTION("MV64x60 watchdog driver");
325MODULE_LICENSE("GPL");
326MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);