blob: 89f326a39b6be72708af83f1257625f33bef6e4d [file] [log] [blame]
Florian Fainelli03ec5852008-02-25 13:11:31 +01001/*
2 * IDT Interprise 79RC32434 watchdog driver
3 *
4 * Copyright (C) 2006, Ondrej Zajicek <santiago@crfreenet.org>
5 * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
6 *
7 * based on
8 * SoftDog 0.05: A Software Watchdog Device
9 *
Alan Cox29fa0582008-10-27 15:17:56 +000010 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * All Rights Reserved.
Florian Fainelli03ec5852008-02-25 13:11:31 +010012 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 */
19
Phil Sutter9b655e02009-02-08 16:44:42 +010020#include <linux/module.h> /* For module specific items */
21#include <linux/moduleparam.h> /* For new moduleparam's */
22#include <linux/types.h> /* For standard types (like size_t) */
23#include <linux/errno.h> /* For the -ENODEV/... values */
24#include <linux/kernel.h> /* For printk/panic/... */
25#include <linux/fs.h> /* For file operations */
26#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV
27 (WATCHDOG_MINOR) */
28#include <linux/watchdog.h> /* For the watchdog specific items */
29#include <linux/init.h> /* For __init/__exit/... */
30#include <linux/platform_device.h> /* For platform_driver framework */
31#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
Florian Fainelli03ec5852008-02-25 13:11:31 +010032
Phil Sutter9b655e02009-02-08 16:44:42 +010033#include <asm/mach-rc32434/integ.h> /* For the Watchdog registers */
34
35#define PFX KBUILD_MODNAME ": "
Florian Fainelli03ec5852008-02-25 13:11:31 +010036
Phil Sutter08eb2e02009-02-08 16:44:42 +010037#define VERSION "0.5"
Florian Fainelli03ec5852008-02-25 13:11:31 +010038
39static struct {
Florian Fainelli03ec5852008-02-25 13:11:31 +010040 unsigned long inuse;
41} rc32434_wdt_device;
42
43static struct integ __iomem *wdt_reg;
Florian Fainelli03ec5852008-02-25 13:11:31 +010044
45static int expect_close;
Phil Sutter0af98d32009-02-08 16:44:42 +010046
47/* Board internal clock speed in Hz,
48 * the watchdog timer ticks at. */
49extern unsigned int idt_cpu_freq;
50
51/* translate wtcompare value to seconds and vice versa */
52#define WTCOMP2SEC(x) (x / idt_cpu_freq)
53#define SEC2WTCOMP(x) (x * idt_cpu_freq)
54
55/* Use a default timeout of 20s. This should be
56 * safe for CPU clock speeds up to 400MHz, as
57 * ((2 ^ 32) - 1) / (400MHz / 2) = 21s. */
58#define WATCHDOG_TIMEOUT 20
59
60static int timeout = WATCHDOG_TIMEOUT;
Phil Sutter08eb2e02009-02-08 16:44:42 +010061module_param(timeout, int, 0);
62MODULE_PARM_DESC(timeout, "Watchdog timeout value, in seconds (default="
63 WATCHDOG_TIMEOUT ")");
Florian Fainelli03ec5852008-02-25 13:11:31 +010064
65static int nowayout = WATCHDOG_NOWAYOUT;
66module_param(nowayout, int, 0);
67MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
68 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
69
Phil Sutter0af98d32009-02-08 16:44:42 +010070/* apply or and nand masks to data read from addr and write back */
71#define SET_BITS(addr, or, nand) \
72 writel((readl(&addr) | or) & ~nand, &addr)
Florian Fainelli03ec5852008-02-25 13:11:31 +010073
Phil Sutter08eb2e02009-02-08 16:44:42 +010074static int rc32434_wdt_set(int new_timeout)
75{
76 int max_to = WTCOMP2SEC((u32)-1);
77
78 if (new_timeout < 0 || new_timeout > max_to) {
79 printk(KERN_ERR PFX "timeout value must be between 0 and %d",
80 max_to);
81 return -EINVAL;
82 }
83 timeout = new_timeout;
84 writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare);
85
86 return 0;
87}
88
Florian Fainelli03ec5852008-02-25 13:11:31 +010089static void rc32434_wdt_start(void)
90{
Phil Sutter0af98d32009-02-08 16:44:42 +010091 u32 or, nand;
Florian Fainelli03ec5852008-02-25 13:11:31 +010092
Phil Sutter0af98d32009-02-08 16:44:42 +010093 /* zero the counter before enabling */
94 writel(0, &wdt_reg->wtcount);
Florian Fainelli03ec5852008-02-25 13:11:31 +010095
Phil Sutter0af98d32009-02-08 16:44:42 +010096 /* don't generate a non-maskable interrupt,
97 * do a warm reset instead */
98 nand = 1 << RC32434_ERR_WNE;
99 or = 1 << RC32434_ERR_WRE;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100100
Phil Sutter0af98d32009-02-08 16:44:42 +0100101 /* reset the ERRCS timeout bit in case it's set */
102 nand |= 1 << RC32434_ERR_WTO;
103
104 SET_BITS(wdt_reg->errcs, or, nand);
105
Phil Sutter08eb2e02009-02-08 16:44:42 +0100106 /* set the timeout (either default or based on module param) */
107 rc32434_wdt_set(timeout);
108
Phil Sutter0af98d32009-02-08 16:44:42 +0100109 /* reset WTC timeout bit and enable WDT */
110 nand = 1 << RC32434_WTC_TO;
111 or = 1 << RC32434_WTC_EN;
112
113 SET_BITS(wdt_reg->wtc, or, nand);
Phil Sutter9b655e02009-02-08 16:44:42 +0100114
115 printk(KERN_INFO PFX "Started watchdog timer.\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100116}
117
118static void rc32434_wdt_stop(void)
119{
Phil Sutter0af98d32009-02-08 16:44:42 +0100120 /* Disable WDT */
121 SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN);
Phil Sutter9b655e02009-02-08 16:44:42 +0100122
123 printk(KERN_INFO PFX "Stopped watchdog timer.\n");
Phil Sutter0af98d32009-02-08 16:44:42 +0100124}
Florian Fainelli03ec5852008-02-25 13:11:31 +0100125
Phil Sutter0af98d32009-02-08 16:44:42 +0100126static void rc32434_wdt_ping(void)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100127{
Florian Fainelli03ec5852008-02-25 13:11:31 +0100128 writel(0, &wdt_reg->wtcount);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100129}
130
131static int rc32434_wdt_open(struct inode *inode, struct file *file)
132{
133 if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
134 return -EBUSY;
135
136 if (nowayout)
137 __module_get(THIS_MODULE);
138
Phil Sutter0af98d32009-02-08 16:44:42 +0100139 rc32434_wdt_start();
140 rc32434_wdt_ping();
141
Florian Fainelli03ec5852008-02-25 13:11:31 +0100142 return nonseekable_open(inode, file);
143}
144
145static int rc32434_wdt_release(struct inode *inode, struct file *file)
146{
Phil Sutter0af98d32009-02-08 16:44:42 +0100147 if (expect_close == 42) {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100148 rc32434_wdt_stop();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100149 module_put(THIS_MODULE);
Phil Sutter0af98d32009-02-08 16:44:42 +0100150 } else {
Phil Sutter9b655e02009-02-08 16:44:42 +0100151 printk(KERN_CRIT PFX
152 "device closed unexpectedly. WDT will not stop!\n");
Phil Sutter0af98d32009-02-08 16:44:42 +0100153 rc32434_wdt_ping();
154 }
Florian Fainelli03ec5852008-02-25 13:11:31 +0100155 clear_bit(0, &rc32434_wdt_device.inuse);
156 return 0;
157}
158
159static ssize_t rc32434_wdt_write(struct file *file, const char *data,
160 size_t len, loff_t *ppos)
161{
162 if (len) {
163 if (!nowayout) {
164 size_t i;
165
166 /* In case it was set long ago */
167 expect_close = 0;
168
169 for (i = 0; i != len; i++) {
170 char c;
171 if (get_user(c, data + i))
172 return -EFAULT;
173 if (c == 'V')
Phil Sutter0af98d32009-02-08 16:44:42 +0100174 expect_close = 42;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100175 }
176 }
Phil Sutter0af98d32009-02-08 16:44:42 +0100177 rc32434_wdt_ping();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100178 return len;
179 }
180 return 0;
181}
182
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000183static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
184 unsigned long arg)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100185{
186 void __user *argp = (void __user *)arg;
187 int new_timeout;
188 unsigned int value;
189 static struct watchdog_info ident = {
190 .options = WDIOF_SETTIMEOUT |
191 WDIOF_KEEPALIVEPING |
192 WDIOF_MAGICCLOSE,
193 .identity = "RC32434_WDT Watchdog",
194 };
195 switch (cmd) {
Phil Sutter9b655e02009-02-08 16:44:42 +0100196 case WDIOC_GETSUPPORT:
197 if (copy_to_user(argp, &ident, sizeof(ident)))
198 return -EFAULT;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100199 break;
200 case WDIOC_GETSTATUS:
201 case WDIOC_GETBOOTSTATUS:
Phil Sutter0af98d32009-02-08 16:44:42 +0100202 value = 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100203 if (copy_to_user(argp, &value, sizeof(int)))
204 return -EFAULT;
205 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100206 case WDIOC_SETOPTIONS:
207 if (copy_from_user(&value, argp, sizeof(int)))
208 return -EFAULT;
209 switch (value) {
210 case WDIOS_ENABLECARD:
211 rc32434_wdt_start();
212 break;
213 case WDIOS_DISABLECARD:
214 rc32434_wdt_stop();
Phil Sutter0af98d32009-02-08 16:44:42 +0100215 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100216 default:
217 return -EINVAL;
218 }
219 break;
Phil Sutter9b655e02009-02-08 16:44:42 +0100220 case WDIOC_KEEPALIVE:
221 rc32434_wdt_ping();
222 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100223 case WDIOC_SETTIMEOUT:
224 if (copy_from_user(&new_timeout, argp, sizeof(int)))
225 return -EFAULT;
Phil Sutter0af98d32009-02-08 16:44:42 +0100226 if (rc32434_wdt_set(new_timeout))
Florian Fainelli03ec5852008-02-25 13:11:31 +0100227 return -EINVAL;
Phil Sutter0af98d32009-02-08 16:44:42 +0100228 /* Fall through */
Florian Fainelli03ec5852008-02-25 13:11:31 +0100229 case WDIOC_GETTIMEOUT:
230 return copy_to_user(argp, &timeout, sizeof(int));
231 default:
232 return -ENOTTY;
233 }
234
235 return 0;
236}
237
238static struct file_operations rc32434_wdt_fops = {
239 .owner = THIS_MODULE,
240 .llseek = no_llseek,
241 .write = rc32434_wdt_write,
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000242 .unlocked_ioctl = rc32434_wdt_ioctl,
Florian Fainelli03ec5852008-02-25 13:11:31 +0100243 .open = rc32434_wdt_open,
244 .release = rc32434_wdt_release,
245};
246
247static struct miscdevice rc32434_wdt_miscdev = {
248 .minor = WATCHDOG_MINOR,
249 .name = "watchdog",
250 .fops = &rc32434_wdt_fops,
251};
252
Phil Sutter9b655e02009-02-08 16:44:42 +0100253static char banner[] __devinitdata = KERN_INFO PFX
254 "Watchdog Timer version " VERSION ", timer margin: %d sec\n";
Florian Fainelli03ec5852008-02-25 13:11:31 +0100255
Phil Sutterd9a87982009-02-08 16:44:42 +0100256static int __devinit rc32434_wdt_probe(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100257{
258 int ret;
259 struct resource *r;
260
Phil Sutter0af98d32009-02-08 16:44:42 +0100261 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100262 if (!r) {
Phil Sutter9b655e02009-02-08 16:44:42 +0100263 printk(KERN_ERR PFX "failed to retrieve resources\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100264 return -ENODEV;
265 }
266
267 wdt_reg = ioremap_nocache(r->start, r->end - r->start);
268 if (!wdt_reg) {
Phil Sutter9b655e02009-02-08 16:44:42 +0100269 printk(KERN_ERR PFX "failed to remap I/O resources\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100270 return -ENXIO;
271 }
272
Phil Sutter08eb2e02009-02-08 16:44:42 +0100273 /* Check that the heartbeat value is within it's range;
274 * if not reset to the default */
275 if (rc32434_wdt_set(timeout)) {
276 rc32434_wdt_set(WATCHDOG_TIMEOUT);
277 printk(KERN_INFO PFX
278 "timeout value must be between 0 and %d\n",
279 WTCOMP2SEC((u32)-1));
280 }
281
Florian Fainelli03ec5852008-02-25 13:11:31 +0100282 ret = misc_register(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100283 if (ret < 0) {
Phil Sutter9b655e02009-02-08 16:44:42 +0100284 printk(KERN_ERR PFX "failed to register watchdog device\n");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100285 goto unmap;
286 }
287
Florian Fainelli03ec5852008-02-25 13:11:31 +0100288 printk(banner, timeout);
289
290 return 0;
291
292unmap:
293 iounmap(wdt_reg);
294 return ret;
295}
296
Phil Sutterd9a87982009-02-08 16:44:42 +0100297static int __devexit rc32434_wdt_remove(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100298{
Florian Fainelli03ec5852008-02-25 13:11:31 +0100299 misc_deregister(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100300 iounmap(wdt_reg);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100301 return 0;
302}
303
Phil Sutter9b655e02009-02-08 16:44:42 +0100304static struct platform_driver rc32434_wdt_driver = {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100305 .probe = rc32434_wdt_probe,
Phil Sutterd9a87982009-02-08 16:44:42 +0100306 .remove = __devexit_p(rc32434_wdt_remove),
307 .driver = {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100308 .name = "rc32434_wdt",
309 }
310};
311
312static int __init rc32434_wdt_init(void)
313{
Phil Sutter9b655e02009-02-08 16:44:42 +0100314 return platform_driver_register(&rc32434_wdt_driver);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100315}
316
317static void __exit rc32434_wdt_exit(void)
318{
Phil Sutter9b655e02009-02-08 16:44:42 +0100319 platform_driver_unregister(&rc32434_wdt_driver);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100320}
321
322module_init(rc32434_wdt_init);
323module_exit(rc32434_wdt_exit);
324
325MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
326 "Florian Fainelli <florian@openwrt.org>");
327MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
328MODULE_LICENSE("GPL");
329MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);