blob: e990e3af8be43f866fe44740fe825ade0fc206c8 [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
James Chapman3be10212005-08-17 09:01:33 +020032/* MV64x60 WDC (config) register access definitions */
33#define MV64x60_WDC_CTL1_MASK (3 << 24)
34#define MV64x60_WDC_CTL1(val) ((val & 3) << 24)
35#define MV64x60_WDC_CTL2_MASK (3 << 26)
36#define MV64x60_WDC_CTL2(val) ((val & 3) << 26)
37
38/* Flags bits */
39#define MV64x60_WDOG_FLAG_OPENED 0
40#define MV64x60_WDOG_FLAG_ENABLED 1
41
42static unsigned long wdt_flags;
43static int wdt_status;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070044static void __iomem *mv64x60_wdt_regs;
James Chapman3be10212005-08-17 09:01:33 +020045static int mv64x60_wdt_timeout;
Dale Farnsworth94796f92007-07-24 11:15:26 -070046static unsigned int bus_clk;
James Chapman3be10212005-08-17 09:01:33 +020047
48static void mv64x60_wdt_reg_write(u32 val)
49{
50 /* Allow write only to CTL1 / CTL2 fields, retaining values in
51 * other fields.
52 */
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070053 u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
James Chapman3be10212005-08-17 09:01:33 +020054 data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
55 data |= val;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070056 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
James Chapman3be10212005-08-17 09:01:33 +020057}
58
59static void mv64x60_wdt_service(void)
60{
61 /* Write 01 followed by 10 to CTL2 */
62 mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01));
63 mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02));
64}
65
66static void mv64x60_wdt_handler_disable(void)
67{
68 if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
69 /* Write 01 followed by 10 to CTL1 */
70 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
71 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
72 printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
73 }
74}
75
76static void mv64x60_wdt_handler_enable(void)
77{
78 if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
79 /* Write 01 followed by 10 to CTL1 */
80 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
81 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
82 printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
83 }
84}
85
Dale Farnsworth94796f92007-07-24 11:15:26 -070086static void mv64x60_wdt_set_timeout(int timeout)
87{
88 /* maximum bus cycle count is 0xFFFFFFFF */
89 if (timeout > 0xFFFFFFFF / bus_clk)
90 timeout = 0xFFFFFFFF / bus_clk;
91
92 mv64x60_wdt_timeout = timeout;
93 writel((timeout * bus_clk) >> 8,
94 mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
95 mv64x60_wdt_service();
96}
97
James Chapman3be10212005-08-17 09:01:33 +020098static int mv64x60_wdt_open(struct inode *inode, struct file *file)
99{
100 if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
101 return -EBUSY;
102
103 mv64x60_wdt_service();
104 mv64x60_wdt_handler_enable();
105
Dale Farnsworth861e513772007-07-24 11:13:26 -0700106 return nonseekable_open(inode, file);
James Chapman3be10212005-08-17 09:01:33 +0200107}
108
109static int mv64x60_wdt_release(struct inode *inode, struct file *file)
110{
111 mv64x60_wdt_service();
112
113#if !defined(CONFIG_WATCHDOG_NOWAYOUT)
114 mv64x60_wdt_handler_disable();
115#endif
116
117 clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
118
119 return 0;
120}
121
Al Virob2846df2005-09-29 00:42:27 +0100122static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
James Chapman3be10212005-08-17 09:01:33 +0200123 size_t len, loff_t * ppos)
124{
James Chapman3be10212005-08-17 09:01:33 +0200125 if (len)
126 mv64x60_wdt_service();
127
128 return len;
129}
130
131static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
132 unsigned int cmd, unsigned long arg)
133{
Dale Farnsworth94796f92007-07-24 11:15:26 -0700134 int timeout;
Al Virob2846df2005-09-29 00:42:27 +0100135 void __user *argp = (void __user *)arg;
James Chapman3be10212005-08-17 09:01:33 +0200136 static struct watchdog_info info = {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700137 .options = WDIOF_SETTIMEOUT |
138 WDIOF_KEEPALIVEPING,
James Chapman3be10212005-08-17 09:01:33 +0200139 .firmware_version = 0,
140 .identity = "MV64x60 watchdog",
141 };
142
143 switch (cmd) {
144 case WDIOC_GETSUPPORT:
Al Virob2846df2005-09-29 00:42:27 +0100145 if (copy_to_user(argp, &info, sizeof(info)))
James Chapman3be10212005-08-17 09:01:33 +0200146 return -EFAULT;
147 break;
148
149 case WDIOC_GETSTATUS:
150 case WDIOC_GETBOOTSTATUS:
Al Virob2846df2005-09-29 00:42:27 +0100151 if (put_user(wdt_status, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200152 return -EFAULT;
153 wdt_status &= ~WDIOF_KEEPALIVEPING;
154 break;
155
156 case WDIOC_GETTEMP:
157 return -EOPNOTSUPP;
158
159 case WDIOC_SETOPTIONS:
160 return -EOPNOTSUPP;
161
162 case WDIOC_KEEPALIVE:
163 mv64x60_wdt_service();
164 wdt_status |= WDIOF_KEEPALIVEPING;
165 break;
166
167 case WDIOC_SETTIMEOUT:
Dale Farnsworth94796f92007-07-24 11:15:26 -0700168 if (get_user(timeout, (int __user *)argp))
169 return -EFAULT;
170 mv64x60_wdt_set_timeout(timeout);
171 /* Fall through */
James Chapman3be10212005-08-17 09:01:33 +0200172
173 case WDIOC_GETTIMEOUT:
Dale Farnsworth264f0992007-07-24 11:14:21 -0700174 if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200175 return -EFAULT;
176 break;
177
178 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200179 return -ENOTTY;
James Chapman3be10212005-08-17 09:01:33 +0200180 }
181
182 return 0;
183}
184
Arjan van de Ven62322d22006-07-03 00:24:21 -0700185static const struct file_operations mv64x60_wdt_fops = {
James Chapman3be10212005-08-17 09:01:33 +0200186 .owner = THIS_MODULE,
187 .llseek = no_llseek,
188 .write = mv64x60_wdt_write,
189 .ioctl = mv64x60_wdt_ioctl,
190 .open = mv64x60_wdt_open,
191 .release = mv64x60_wdt_release,
192};
193
194static struct miscdevice mv64x60_wdt_miscdev = {
195 .minor = WATCHDOG_MINOR,
196 .name = "watchdog",
197 .fops = &mv64x60_wdt_fops,
198};
199
Russell King3ae5eae2005-11-09 22:32:44 +0000200static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200201{
Russell King3ae5eae2005-11-09 22:32:44 +0000202 struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700203 struct resource *r;
Dale Farnsworth94796f92007-07-24 11:15:26 -0700204 int timeout = 10;
James Chapman3be10212005-08-17 09:01:33 +0200205
Dale Farnsworth94796f92007-07-24 11:15:26 -0700206 bus_clk = 133; /* in MHz */
James Chapman3be10212005-08-17 09:01:33 +0200207 if (pdata) {
Dale Farnsworth94796f92007-07-24 11:15:26 -0700208 timeout = pdata->timeout;
James Chapman3be10212005-08-17 09:01:33 +0200209 bus_clk = pdata->bus_clk;
210 }
211
Dale Farnsworth94796f92007-07-24 11:15:26 -0700212 /* Since bus_clk is truncated MHz, actual frequency could be
213 * up to 1MHz higher. Round up, since it's better to time out
214 * too late than too soon.
215 */
216 bus_clk++;
217 bus_clk *= 1000000; /* convert to Hz */
218
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700219 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
220 if (!r)
221 return -ENODEV;
222
223 mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1);
224 if (mv64x60_wdt_regs == NULL)
225 return -ENOMEM;
James Chapman3be10212005-08-17 09:01:33 +0200226
Dale Farnsworth94796f92007-07-24 11:15:26 -0700227 mv64x60_wdt_set_timeout(timeout);
James Chapman3be10212005-08-17 09:01:33 +0200228
229 return misc_register(&mv64x60_wdt_miscdev);
230}
231
Russell King3ae5eae2005-11-09 22:32:44 +0000232static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200233{
234 misc_deregister(&mv64x60_wdt_miscdev);
235
236 mv64x60_wdt_service();
237 mv64x60_wdt_handler_disable();
238
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700239 iounmap(mv64x60_wdt_regs);
240
James Chapman3be10212005-08-17 09:01:33 +0200241 return 0;
242}
243
Russell King3ae5eae2005-11-09 22:32:44 +0000244static struct platform_driver mv64x60_wdt_driver = {
James Chapman3be10212005-08-17 09:01:33 +0200245 .probe = mv64x60_wdt_probe,
246 .remove = __devexit_p(mv64x60_wdt_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000247 .driver = {
248 .owner = THIS_MODULE,
249 .name = MV64x60_WDT_NAME,
250 },
James Chapman3be10212005-08-17 09:01:33 +0200251};
252
James Chapman3be10212005-08-17 09:01:33 +0200253static int __init mv64x60_wdt_init(void)
254{
James Chapman3be10212005-08-17 09:01:33 +0200255 printk(KERN_INFO "MV64x60 watchdog driver\n");
256
Dale Farnsworth422db8d2007-07-24 11:07:38 -0700257 return platform_driver_register(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200258}
259
260static void __exit mv64x60_wdt_exit(void)
261{
Russell King3ae5eae2005-11-09 22:32:44 +0000262 platform_driver_unregister(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200263}
264
265module_init(mv64x60_wdt_init);
266module_exit(mv64x60_wdt_exit);
267
268MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
269MODULE_DESCRIPTION("MV64x60 watchdog driver");
270MODULE_LICENSE("GPL");
271MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);