2 * mv64x60_wdt.c - MV64X60 (Marvell Discovery) watchdog userspace interface
4 * Author: James Chapman <jchapman@katalix.com>
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.
10 * Derived from mpc8xx_wdt.c, with the following copyright.
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
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>
24 #include <linux/platform_device.h>
26 #include <linux/mv643xx.h>
27 #include <linux/uaccess.h>
30 #define MV64x60_WDT_WDC_OFFSET 0
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.
41 * The low 24 bits contain the timer reload value.
43 #define MV64x60_WDC_ENABLE_SHIFT 24
44 #define MV64x60_WDC_SERVICE_SHIFT 26
45 #define MV64x60_WDC_ENABLED_SHIFT 31
47 #define MV64x60_WDC_ENABLED_TRUE 1
48 #define MV64x60_WDC_ENABLED_FALSE 0
51 #define MV64x60_WDOG_FLAG_OPENED 0
53 static unsigned long wdt_flags;
54 static int wdt_status;
55 static void __iomem *mv64x60_wdt_regs;
56 static int mv64x60_wdt_timeout;
57 static int mv64x60_wdt_count;
58 static unsigned int bus_clk;
59 static char expect_close;
60 static DEFINE_SPINLOCK(mv64x60_wdt_spinlock);
62 static int nowayout = WATCHDOG_NOWAYOUT;
63 module_param(nowayout, int, 0);
64 MODULE_PARM_DESC(nowayout,
65 "Watchdog cannot be stopped once started (default="
66 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
68 static int mv64x60_wdt_toggle_wdc(int enabled_predicate, int field_shift)
74 spin_lock(&mv64x60_wdt_spinlock);
75 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
76 enabled = (data >> MV64x60_WDC_ENABLED_SHIFT) & 1;
78 /* only toggle the requested field if enabled state matches predicate */
79 if ((enabled ^ enabled_predicate) == 0) {
80 /* We write a 1, then a 2 -- to the appropriate field */
81 data = (1 << field_shift) | mv64x60_wdt_count;
82 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
84 data = (2 << field_shift) | mv64x60_wdt_count;
85 writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
88 spin_unlock(&mv64x60_wdt_spinlock);
93 static void mv64x60_wdt_service(void)
95 mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
96 MV64x60_WDC_SERVICE_SHIFT);
99 static void mv64x60_wdt_handler_enable(void)
101 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_FALSE,
102 MV64x60_WDC_ENABLE_SHIFT)) {
103 mv64x60_wdt_service();
104 printk(KERN_NOTICE "mv64x60_wdt: watchdog activated\n");
108 static void mv64x60_wdt_handler_disable(void)
110 if (mv64x60_wdt_toggle_wdc(MV64x60_WDC_ENABLED_TRUE,
111 MV64x60_WDC_ENABLE_SHIFT))
112 printk(KERN_NOTICE "mv64x60_wdt: watchdog deactivated\n");
115 static void mv64x60_wdt_set_timeout(unsigned int timeout)
117 /* maximum bus cycle count is 0xFFFFFFFF */
118 if (timeout > 0xFFFFFFFF / bus_clk)
119 timeout = 0xFFFFFFFF / bus_clk;
121 mv64x60_wdt_count = timeout * bus_clk >> 8;
122 mv64x60_wdt_timeout = timeout;
125 static int mv64x60_wdt_open(struct inode *inode, struct file *file)
127 if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
131 __module_get(THIS_MODULE);
133 mv64x60_wdt_handler_enable();
135 return nonseekable_open(inode, file);
138 static int mv64x60_wdt_release(struct inode *inode, struct file *file)
140 if (expect_close == 42)
141 mv64x60_wdt_handler_disable();
144 "mv64x60_wdt: unexpected close, not stopping timer!\n");
145 mv64x60_wdt_service();
149 clear_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags);
154 static ssize_t mv64x60_wdt_write(struct file *file, const char __user *data,
155 size_t len, loff_t *ppos)
163 for (i = 0; i != len; i++) {
165 if (get_user(c, data + i))
171 mv64x60_wdt_service();
177 static long mv64x60_wdt_ioctl(struct file *file,
178 unsigned int cmd, unsigned long arg)
182 void __user *argp = (void __user *)arg;
183 static struct watchdog_info info = {
184 .options = WDIOF_SETTIMEOUT |
187 .firmware_version = 0,
188 .identity = "MV64x60 watchdog",
192 case WDIOC_GETSUPPORT:
193 if (copy_to_user(argp, &info, sizeof(info)))
197 case WDIOC_GETSTATUS:
198 case WDIOC_GETBOOTSTATUS:
199 if (put_user(wdt_status, (int __user *)argp))
201 wdt_status &= ~WDIOF_KEEPALIVEPING;
207 case WDIOC_SETOPTIONS:
208 if (get_user(options, (int __user *)argp))
211 if (options & WDIOS_DISABLECARD)
212 mv64x60_wdt_handler_disable();
214 if (options & WDIOS_ENABLECARD)
215 mv64x60_wdt_handler_enable();
218 case WDIOC_KEEPALIVE:
219 mv64x60_wdt_service();
220 wdt_status |= WDIOF_KEEPALIVEPING;
223 case WDIOC_SETTIMEOUT:
224 if (get_user(timeout, (int __user *)argp))
226 mv64x60_wdt_set_timeout(timeout);
229 case WDIOC_GETTIMEOUT:
230 if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
241 static const struct file_operations mv64x60_wdt_fops = {
242 .owner = THIS_MODULE,
244 .write = mv64x60_wdt_write,
245 .unlocked_ioctl = mv64x60_wdt_ioctl,
246 .open = mv64x60_wdt_open,
247 .release = mv64x60_wdt_release,
250 static struct miscdevice mv64x60_wdt_miscdev = {
251 .minor = WATCHDOG_MINOR,
253 .fops = &mv64x60_wdt_fops,
256 static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
258 struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
262 bus_clk = 133; /* in MHz */
264 timeout = pdata->timeout;
265 bus_clk = pdata->bus_clk;
268 /* 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.
273 bus_clk *= 1000000; /* convert to Hz */
275 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
279 mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1);
280 if (mv64x60_wdt_regs == NULL)
283 mv64x60_wdt_set_timeout(timeout);
285 mv64x60_wdt_handler_disable(); /* in case timer was already running */
287 return misc_register(&mv64x60_wdt_miscdev);
290 static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
292 misc_deregister(&mv64x60_wdt_miscdev);
294 mv64x60_wdt_handler_disable();
296 iounmap(mv64x60_wdt_regs);
301 static struct platform_driver mv64x60_wdt_driver = {
302 .probe = mv64x60_wdt_probe,
303 .remove = __devexit_p(mv64x60_wdt_remove),
305 .owner = THIS_MODULE,
306 .name = MV64x60_WDT_NAME,
310 static int __init mv64x60_wdt_init(void)
312 printk(KERN_INFO "MV64x60 watchdog driver\n");
314 return platform_driver_register(&mv64x60_wdt_driver);
317 static void __exit mv64x60_wdt_exit(void)
319 platform_driver_unregister(&mv64x60_wdt_driver);
322 module_init(mv64x60_wdt_init);
323 module_exit(mv64x60_wdt_exit);
325 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
326 MODULE_DESCRIPTION("MV64x60 watchdog driver");
327 MODULE_LICENSE("GPL");
328 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
329 MODULE_ALIAS("platform:" MV64x60_WDT_NAME);