blob: 1ad632dd03e654b3a6633793d9048a2ae07d686a [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
James Chapman3be10212005-08-17 09:01:33 +020026#include <asm/mv64x60.h>
27#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;
46
47static void mv64x60_wdt_reg_write(u32 val)
48{
49 /* Allow write only to CTL1 / CTL2 fields, retaining values in
50 * other fields.
51 */
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070052 u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
James Chapman3be10212005-08-17 09:01:33 +020053 data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
54 data |= val;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -070055 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
James Chapman3be10212005-08-17 09:01:33 +020056}
57
58static void mv64x60_wdt_service(void)
59{
60 /* Write 01 followed by 10 to CTL2 */
61 mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x01));
62 mv64x60_wdt_reg_write(MV64x60_WDC_CTL2(0x02));
63}
64
65static void mv64x60_wdt_handler_disable(void)
66{
67 if (test_and_clear_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
68 /* Write 01 followed by 10 to CTL1 */
69 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
70 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
71 printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
72 }
73}
74
75static void mv64x60_wdt_handler_enable(void)
76{
77 if (!test_and_set_bit(MV64x60_WDOG_FLAG_ENABLED, &wdt_flags)) {
78 /* Write 01 followed by 10 to CTL1 */
79 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x01));
80 mv64x60_wdt_reg_write(MV64x60_WDC_CTL1(0x02));
81 printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
82 }
83}
84
85static int mv64x60_wdt_open(struct inode *inode, struct file *file)
86{
87 if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
88 return -EBUSY;
89
90 mv64x60_wdt_service();
91 mv64x60_wdt_handler_enable();
92
Al Virob2846df2005-09-29 00:42:27 +010093 nonseekable_open(inode, file);
94
James Chapman3be10212005-08-17 09:01:33 +020095 return 0;
96}
97
98static int mv64x60_wdt_release(struct inode *inode, struct file *file)
99{
100 mv64x60_wdt_service();
101
102#if !defined(CONFIG_WATCHDOG_NOWAYOUT)
103 mv64x60_wdt_handler_disable();
104#endif
105
106 clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
107
108 return 0;
109}
110
Al Virob2846df2005-09-29 00:42:27 +0100111static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
James Chapman3be10212005-08-17 09:01:33 +0200112 size_t len, loff_t * ppos)
113{
James Chapman3be10212005-08-17 09:01:33 +0200114 if (len)
115 mv64x60_wdt_service();
116
117 return len;
118}
119
120static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
121 unsigned int cmd, unsigned long arg)
122{
123 int timeout;
Al Virob2846df2005-09-29 00:42:27 +0100124 void __user *argp = (void __user *)arg;
James Chapman3be10212005-08-17 09:01:33 +0200125 static struct watchdog_info info = {
126 .options = WDIOF_KEEPALIVEPING,
127 .firmware_version = 0,
128 .identity = "MV64x60 watchdog",
129 };
130
131 switch (cmd) {
132 case WDIOC_GETSUPPORT:
Al Virob2846df2005-09-29 00:42:27 +0100133 if (copy_to_user(argp, &info, sizeof(info)))
James Chapman3be10212005-08-17 09:01:33 +0200134 return -EFAULT;
135 break;
136
137 case WDIOC_GETSTATUS:
138 case WDIOC_GETBOOTSTATUS:
Al Virob2846df2005-09-29 00:42:27 +0100139 if (put_user(wdt_status, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200140 return -EFAULT;
141 wdt_status &= ~WDIOF_KEEPALIVEPING;
142 break;
143
144 case WDIOC_GETTEMP:
145 return -EOPNOTSUPP;
146
147 case WDIOC_SETOPTIONS:
148 return -EOPNOTSUPP;
149
150 case WDIOC_KEEPALIVE:
151 mv64x60_wdt_service();
152 wdt_status |= WDIOF_KEEPALIVEPING;
153 break;
154
155 case WDIOC_SETTIMEOUT:
156 return -EOPNOTSUPP;
157
158 case WDIOC_GETTIMEOUT:
159 timeout = mv64x60_wdt_timeout * HZ;
Al Virob2846df2005-09-29 00:42:27 +0100160 if (put_user(timeout, (int __user *)argp))
James Chapman3be10212005-08-17 09:01:33 +0200161 return -EFAULT;
162 break;
163
164 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200165 return -ENOTTY;
James Chapman3be10212005-08-17 09:01:33 +0200166 }
167
168 return 0;
169}
170
Arjan van de Ven62322d22006-07-03 00:24:21 -0700171static const struct file_operations mv64x60_wdt_fops = {
James Chapman3be10212005-08-17 09:01:33 +0200172 .owner = THIS_MODULE,
173 .llseek = no_llseek,
174 .write = mv64x60_wdt_write,
175 .ioctl = mv64x60_wdt_ioctl,
176 .open = mv64x60_wdt_open,
177 .release = mv64x60_wdt_release,
178};
179
180static struct miscdevice mv64x60_wdt_miscdev = {
181 .minor = WATCHDOG_MINOR,
182 .name = "watchdog",
183 .fops = &mv64x60_wdt_fops,
184};
185
Russell King3ae5eae2005-11-09 22:32:44 +0000186static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200187{
Russell King3ae5eae2005-11-09 22:32:44 +0000188 struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
James Chapman3be10212005-08-17 09:01:33 +0200189 int bus_clk = 133;
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700190 struct resource *r;
James Chapman3be10212005-08-17 09:01:33 +0200191
192 mv64x60_wdt_timeout = 10;
193 if (pdata) {
194 mv64x60_wdt_timeout = pdata->timeout;
195 bus_clk = pdata->bus_clk;
196 }
197
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700198 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
199 if (!r)
200 return -ENODEV;
201
202 mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1);
203 if (mv64x60_wdt_regs == NULL)
204 return -ENOMEM;
James Chapman3be10212005-08-17 09:01:33 +0200205
206 writel((mv64x60_wdt_timeout * (bus_clk * 1000000)) >> 8,
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700207 mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
James Chapman3be10212005-08-17 09:01:33 +0200208
209 return misc_register(&mv64x60_wdt_miscdev);
210}
211
Russell King3ae5eae2005-11-09 22:32:44 +0000212static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
James Chapman3be10212005-08-17 09:01:33 +0200213{
214 misc_deregister(&mv64x60_wdt_miscdev);
215
216 mv64x60_wdt_service();
217 mv64x60_wdt_handler_disable();
218
Dale Farnsworth8a5cfa62007-07-24 11:09:18 -0700219 iounmap(mv64x60_wdt_regs);
220
James Chapman3be10212005-08-17 09:01:33 +0200221 return 0;
222}
223
Russell King3ae5eae2005-11-09 22:32:44 +0000224static struct platform_driver mv64x60_wdt_driver = {
James Chapman3be10212005-08-17 09:01:33 +0200225 .probe = mv64x60_wdt_probe,
226 .remove = __devexit_p(mv64x60_wdt_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000227 .driver = {
228 .owner = THIS_MODULE,
229 .name = MV64x60_WDT_NAME,
230 },
James Chapman3be10212005-08-17 09:01:33 +0200231};
232
James Chapman3be10212005-08-17 09:01:33 +0200233static int __init mv64x60_wdt_init(void)
234{
James Chapman3be10212005-08-17 09:01:33 +0200235 printk(KERN_INFO "MV64x60 watchdog driver\n");
236
Dale Farnsworth422db8d2007-07-24 11:07:38 -0700237 return platform_driver_register(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200238}
239
240static void __exit mv64x60_wdt_exit(void)
241{
Russell King3ae5eae2005-11-09 22:32:44 +0000242 platform_driver_unregister(&mv64x60_wdt_driver);
James Chapman3be10212005-08-17 09:01:33 +0200243}
244
245module_init(mv64x60_wdt_init);
246module_exit(mv64x60_wdt_exit);
247
248MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
249MODULE_DESCRIPTION("MV64x60 watchdog driver");
250MODULE_LICENSE("GPL");
251MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);