blob: c53d025e70dfce053b6f05835d737605fa7f4669 [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
Joe Perches27c766a2012-02-15 15:06:19 -080018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
James Chapman3be10212005-08-17 09:01:33 +020020#include <linux/fs.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/miscdevice.h>
24#include <linux/module.h>
25#include <linux/watchdog.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010026#include <linux/platform_device.h>
Dale Farnsworth7e07a152007-07-24 11:12:24 -070027#include <linux/mv643xx.h>
Alan Coxa86b8492008-05-19 14:07:26 +010028#include <linux/uaccess.h>
29#include <linux/io.h>
James Chapman3be10212005-08-17 09:01:33 +020030
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070031#define MV64x60_WDT_WDC_OFFSET 0
32
Dale Farnsworthf1869992007-07-24 11:31:25 -070033/*
34 * The watchdog configuration register contains a pair of 2-bit fields,
35 * 1. a reload field, bits 27-26, which triggers a reload of
36 * the countdown register, and
37 * 2. an enable field, bits 25-24, which toggles between
38 * enabling and disabling the watchdog timer.
39 * Bit 31 is a read-only field which indicates whether the
40 * watchdog timer is currently enabled.
41 *
42 * The low 24 bits contain the timer reload value.
43 */
44#define MV64x60_WDC_ENABLE_SHIFT 24
45#define MV64x60_WDC_SERVICE_SHIFT 26
46#define MV64x60_WDC_ENABLED_SHIFT 31
47
48#define MV64x60_WDC_ENABLED_TRUE 1
49#define MV64x60_WDC_ENABLED_FALSE 0
James Chapman3be10212005-08-17 09:01:33 +020050
51/* Flags bits */
52#define MV64x60_WDOG_FLAG_OPENED 0
James Chapman3be10212005-08-17 09:01:33 +020053
54static unsigned long wdt_flags;
55static int wdt_status;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070056static void __iomem *mv64x60_wdt_regs;
James Chapman3be10212005-08-17 09:01:33 +020057static int mv64x60_wdt_timeout;
Dale Farnsworthf1869992007-07-24 11:31:25 -070058static int mv64x60_wdt_count;
Dale Farnsworth94796f92007-07-24 11:15:26 -070059static unsigned int bus_clk;
Dale Farnsworthbf2fc922007-07-24 11:18:14 -070060static char expect_close;
Dale Farnsworthf1869992007-07-24 11:31:25 -070061static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
James Chapman3be10212005-08-17 09:01:33 +020062
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010063static bool nowayout = WATCHDOG_NOWAYOUT;
64module_param(nowayout, bool, 0);
Alan Coxa86b8492008-05-19 14:07:26 +010065MODULE_PARM_DESC(nowayout,
66 "Watchdog cannot be stopped once started (default="
67 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Dale Farnsworthd37a5c32007-07-24 11:17:23 -070068
Dale Farnsworthf1869992007-07-24 11:31:25 -070069static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
James Chapman3be10212005-08-17 09:01:33 +020070{
Dale Farnsworthf1869992007-07-24 11:31:25 -070071 u32 data;
72 u32 enabled;
73 int ret = 0;
74
75 spin_lock(&mv64x60_wdt_spinlock);
76 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
77 enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
78
79 /* only toggle the requested field if enabled state matches predicate */
80 if ((enabled ^ enabled_predicate) == 0) {
81 /* We write a 1, then a 2 -- to the appropriate field */
82 data = (1 << field_shift) | mv64x60_wdt_count;
83 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
84
85 data = (2 << field_shift) | mv64x60_wdt_count;
86 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
87 ret = 1;
88 }
89 spin_unlock(&mv64x60_wdt_spinlock);
90
91 return ret;
James Chapman3be10212005-08-17 09:01:33 +020092}
93
94static void mv64x60_wdt_service(void)
95{
Dale Farnsworthf1869992007-07-24 11:31:25 -070096 mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
97 MV64x60_WDC_SERVICE_SHIFT);
James Chapman3be10212005-08-17 09:01:33 +020098}
99
100static void mv64x60_wdt_handler_enable(void)
101{
Dale Farnsworthf1869992007-07-24 11:31:25 -0700102 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
103 MV64x60_WDC_ENABLE_SHIFT)) {
104 mv64x60_wdt_service();
Joe Perches27c766a2012-02-15 15:06:19 -0800105 pr_notice("watchdog activated\n");
James Chapman3be10212005-08-17 09:01:33 +0200106 }
107}
108
Dale Farnsworthf1869992007-07-24 11:31:25 -0700109static void mv64x60_wdt_handler_disable(void)
110{
111 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
112 MV64x60_WDC_ENABLE_SHIFT))
Joe Perches27c766a2012-02-15 15:06:19 -0800113 pr_notice("watchdog deactivated\n");
Dale Farnsworthf1869992007-07-24 11:31:25 -0700114}
115
116static void mv64x60_wdt_set_timeout(unsigned int timeout)
Dale Farnsworth94796f92007-07-24 11:15:26 -0700117{
118 /* maximum bus cycle count is 0xFFFFFFFF */
119 if (timeout > 0xFFFFFFFF / bus_clk)
120 timeout = 0xFFFFFFFF / bus_clk;
121
Dale Farnsworthf1869992007-07-24 11:31:25 -0700122 mv64x60_wdt_count = timeout * bus_clk >> 8;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700123 mv64x60_wdt_timeout = timeout;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700124}
125
James Chapman3be10212005-08-17 09:01:33 +0200126static int mv64x60_wdt_open(struct inode *inode, struct file *file)
127{
128 if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
129 return -EBUSY;
130
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700131 if (nowayout)
132 __module_get(THIS_MODULE);
133
James Chapman3be10212005-08-17 09:01:33 +0200134 mv64x60_wdt_handler_enable();
135
Dale Farnsworth861e513772007-07-24 11:13:26 -0700136 return nonseekable_open(inode, file);
James Chapman3be10212005-08-17 09:01:33 +0200137}
138
139static int mv64x60_wdt_release(struct inode *inode, struct file *file)
140{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700141 if (expect_close == 42)
Dale Farnsworthd37a5c32007-07-24 11:17:23 -0700142 mv64x60_wdt_handler_disable();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700143 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800144 pr_crit("unexpected close, not stopping timer!\n");
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700145 mv64x60_wdt_service();
146 }
147 expect_close = 0;
James Chapman3be10212005-08-17 09:01:33 +0200148
149 clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
150
151 return 0;
152}
153
Al Virob2846df2005-09-29 00:42:27 +0100154static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
Alan Coxa86b8492008-05-19 14:07:26 +0100155 size_t len, loff_t *ppos)
James Chapman3be10212005-08-17 09:01:33 +0200156{
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700157 if (len) {
158 if (!nowayout) {
159 size_t i;
160
161 expect_close = 0;
162
163 for (i = 0; i != len; i++) {
164 char c;
Alan Coxa86b8492008-05-19 14:07:26 +0100165 if (get_user(c, data + i))
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700166 return -EFAULT;
167 if (c == 'V')
168 expect_close = 42;
169 }
170 }
James Chapman3be10212005-08-17 09:01:33 +0200171 mv64x60_wdt_service();
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700172 }
James Chapman3be10212005-08-17 09:01:33 +0200173
174 return len;
175}
176
Alan Coxa86b8492008-05-19 14:07:26 +0100177static long mv64x60_wdt_ioctl(struct file *file,
178 unsigned int cmd, unsigned long arg)
James Chapman3be10212005-08-17 09:01:33 +0200179{
Dale Farnsworth94796f92007-07-24 11:15:26 -0700180 int timeout;
Dale Farnsworth85d57232007-07-24 11:16:29 -0700181 int options;
Al Virob2846df2005-09-29 00:42:27 +0100182 void __user *argp = (void __user *)arg;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000183 static const struct watchdog_info info = {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700184 .options = WDIOF_SETTIMEOUT |
Dale Farnsworthbf2fc922007-07-24 11:18:14 -0700185 WDIOF_MAGICCLOSE |
Dale Farnsworth94796f92007-07-24 11:15:26 -0700186 WDIOF_KEEPALIVEPING,
James Chapman3be10212005-08-17 09:01:33 +0200187 .firmware_version = 0,
188 .identity = "MV64x60 watchdog",
189 };
190
191 switch (cmd) {
192 case WDIOC_GETSUPPORT:
Al Virob2846df2005-09-29 00:42:27 +0100193 if (copy_to_user(argp, &info, sizeof(info)))
James Chapman3be10212005-08-17 09:01:33 +0200194 return -EFAULT;
195 break;
196
197 case WDIOC_GETSTATUS:
198 case WDIOC_GETBOOTSTATUS:
Al Virob2846df2005-09-29 00:42:27 +0100199 if (put_user(wdt_status, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200200 return -EFAULT;
201 wdt_status &= ~WDIOF_KEEPALIVEPING;
202 break;
203
204 case WDIOC_GETTEMP:
205 return -EOPNOTSUPP;
206
207 case WDIOC_SETOPTIONS:
Dale Farnsworth85d57232007-07-24 11:16:29 -0700208 if (get_user(options, (int __user *)argp))
209 return -EFAULT;
210
211 if (options & WDIOS_DISABLECARD)
212 mv64x60_wdt_handler_disable();
213
214 if (options & WDIOS_ENABLECARD)
215 mv64x60_wdt_handler_enable();
216 break;
James Chapman3be10212005-08-17 09:01:33 +0200217
218 case WDIOC_KEEPALIVE:
219 mv64x60_wdt_service();
220 wdt_status |= WDIOF_KEEPALIVEPING;
221 break;
222
223 case WDIOC_SETTIMEOUT:
Dale Farnsworth94796f92007-07-24 11:15:26 -0700224 if (get_user(timeout, (int __user *)argp))
225 return -EFAULT;
226 mv64x60_wdt_set_timeout(timeout);
227 /* Fall through */
James Chapman3be10212005-08-17 09:01:33 +0200228
229 case WDIOC_GETTIMEOUT:
Dale Farnsworth264f0992007-07-24 11:14:21 -0700230 if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200231 return -EFAULT;
232 break;
233
234 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200235 return -ENOTTY;
James Chapman3be10212005-08-17 09:01:33 +0200236 }
237
238 return 0;
239}
240
Arjan van de Ven62322d22006-07-03 00:24:21 -0700241static const struct file_operations mv64x60_wdt_fops = {
James Chapman3be10212005-08-17 09:01:33 +0200242 .owner = THIS_MODULE,
243 .llseek = no_llseek,
244 .write = mv64x60_wdt_write,
Alan Coxa86b8492008-05-19 14:07:26 +0100245 .unlocked_ioctl = mv64x60_wdt_ioctl,
James Chapman3be10212005-08-17 09:01:33 +0200246 .open = mv64x60_wdt_open,
247 .release = mv64x60_wdt_release,
248};
249
250static struct miscdevice mv64x60_wdt_miscdev = {
251 .minor = WATCHDOG_MINOR,
252 .name = "watchdog",
253 .fops = &mv64x60_wdt_fops,
254};
255
Russell King3ae5eae2005-11-09 22:32:44 +0000256static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200257{
Russell King3ae5eae2005-11-09 22:32:44 +0000258 struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700259 struct resource *r;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700260 int timeout = 10;
James Chapman3be10212005-08-17 09:01:33 +0200261
Dale Farnsworth94796f92007-07-24 11:15:26 -0700262 bus_clk = 133; /* in MHz */
James Chapman3be10212005-08-17 09:01:33 +0200263 if (pdata) {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700264 timeout = pdata->timeout;
James Chapman3be10212005-08-17 09:01:33 +0200265 bus_clk = pdata->bus_clk;
266 }
267
Dale Farnsworth94796f92007-07-24 11:15:26 -0700268 /* Since bus_clk is truncated MHz, actual frequency could be
269 * up to 1MHz higher. Round up, since it's better to time out
270 * too late than too soon.
271 */
272 bus_clk++;
273 bus_clk *= 1000000; /* convert to Hz */
274
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700275 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
276 if (!r)
277 return -ENODEV;
278
H Hartley Sweetenb782a562009-12-04 12:24:04 -0500279 mv64x60_wdt_regs = ioremap(r->start, resource_size(r));
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700280 if (mv64x60_wdt_regs == NULL)
281 return -ENOMEM;
James Chapman3be10212005-08-17 09:01:33 +0200282
Dale Farnsworth94796f92007-07-24 11:15:26 -0700283 mv64x60_wdt_set_timeout(timeout);
James Chapman3be10212005-08-17 09:01:33 +0200284
Dale Farnsworth2422df52007-07-24 11:19:47 -0700285 mv64x60_wdt_handler_disable(); /* in case timer was already running */
286
James Chapman3be10212005-08-17 09:01:33 +0200287 return misc_register(&mv64x60_wdt_miscdev);
288}
289
Russell King3ae5eae2005-11-09 22:32:44 +0000290static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200291{
292 misc_deregister(&mv64x60_wdt_miscdev);
293
James Chapman3be10212005-08-17 09:01:33 +0200294 mv64x60_wdt_handler_disable();
295
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700296 iounmap(mv64x60_wdt_regs);
297
James Chapman3be10212005-08-17 09:01:33 +0200298 return 0;
299}
300
Russell King3ae5eae2005-11-09 22:32:44 +0000301static struct platform_driver mv64x60_wdt_driver = {
James Chapman3be10212005-08-17 09:01:33 +0200302 .probe = mv64x60_wdt_probe,
303 .remove = __devexit_p(mv64x60_wdt_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000304 .driver = {
305 .owner = THIS_MODULE,
306 .name = MV64x60_WDT_NAME,
307 },
James Chapman3be10212005-08-17 09:01:33 +0200308};
309
James Chapman3be10212005-08-17 09:01:33 +0200310static int __init mv64x60_wdt_init(void)
311{
Joe Perches27c766a2012-02-15 15:06:19 -0800312 pr_info("MV64x60 watchdog driver\n");
James Chapman3be10212005-08-17 09:01:33 +0200313
Dale Farnsworth422db8d2007-07-24 11:07:38 -0700314 return platform_driver_register(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200315}
316
317static void __exit mv64x60_wdt_exit(void)
318{
Russell King3ae5eae2005-11-09 22:32:44 +0000319 platform_driver_unregister(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200320}
321
322module_init(mv64x60_wdt_init);
323module_exit(mv64x60_wdt_exit);
324
325MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
326MODULE_DESCRIPTION("MV64x60 watchdog driver");
327MODULE_LICENSE("GPL");
328MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700329MODULE_ALIAS("platform:" MV64x60_WDT_NAME);