blob: f3553fa40b17f912d223220c163343c2e4895597 [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
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/fs.h>
24#include <linux/mm.h>
25#include <linux/miscdevice.h>
26#include <linux/watchdog.h>
27#include <linux/reboot.h>
28#include <linux/smp_lock.h>
29#include <linux/init.h>
30#include <linux/platform_device.h>
31#include <linux/uaccess.h>
32
33#include <asm/bootinfo.h>
34#include <asm/time.h>
35#include <asm/mach-rc32434/integ.h>
36
Phil Sutterd9a87982009-02-08 16:44:42 +010037#define VERSION "0.4"
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;
Florian Fainelli03ec5852008-02-25 13:11:31 +010061
62static int nowayout = WATCHDOG_NOWAYOUT;
63module_param(nowayout, int, 0);
64MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
65 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
66
Phil Sutter0af98d32009-02-08 16:44:42 +010067/* apply or and nand masks to data read from addr and write back */
68#define SET_BITS(addr, or, nand) \
69 writel((readl(&addr) | or) & ~nand, &addr)
Florian Fainelli03ec5852008-02-25 13:11:31 +010070
71static void rc32434_wdt_start(void)
72{
Phil Sutter0af98d32009-02-08 16:44:42 +010073 u32 or, nand;
Florian Fainelli03ec5852008-02-25 13:11:31 +010074
Phil Sutter0af98d32009-02-08 16:44:42 +010075 /* zero the counter before enabling */
76 writel(0, &wdt_reg->wtcount);
Florian Fainelli03ec5852008-02-25 13:11:31 +010077
Phil Sutter0af98d32009-02-08 16:44:42 +010078 /* don't generate a non-maskable interrupt,
79 * do a warm reset instead */
80 nand = 1 << RC32434_ERR_WNE;
81 or = 1 << RC32434_ERR_WRE;
Florian Fainelli03ec5852008-02-25 13:11:31 +010082
Phil Sutter0af98d32009-02-08 16:44:42 +010083 /* reset the ERRCS timeout bit in case it's set */
84 nand |= 1 << RC32434_ERR_WTO;
85
86 SET_BITS(wdt_reg->errcs, or, nand);
87
88 /* reset WTC timeout bit and enable WDT */
89 nand = 1 << RC32434_WTC_TO;
90 or = 1 << RC32434_WTC_EN;
91
92 SET_BITS(wdt_reg->wtc, or, nand);
Florian Fainelli03ec5852008-02-25 13:11:31 +010093}
94
95static void rc32434_wdt_stop(void)
96{
Phil Sutter0af98d32009-02-08 16:44:42 +010097 /* Disable WDT */
98 SET_BITS(wdt_reg->wtc, 0, 1 << RC32434_WTC_EN);
99}
Florian Fainelli03ec5852008-02-25 13:11:31 +0100100
Phil Sutter0af98d32009-02-08 16:44:42 +0100101static int rc32434_wdt_set(int new_timeout)
102{
103 int max_to = WTCOMP2SEC((u32)-1);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100104
Phil Sutter0af98d32009-02-08 16:44:42 +0100105 if (new_timeout < 0 || new_timeout > max_to) {
106 printk(KERN_ERR KBUILD_MODNAME
107 ": timeout value must be between 0 and %d",
108 max_to);
109 return -EINVAL;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100110 }
Florian Fainelli03ec5852008-02-25 13:11:31 +0100111 timeout = new_timeout;
Phil Sutter0af98d32009-02-08 16:44:42 +0100112 writel(SEC2WTCOMP(timeout), &wdt_reg->wtcompare);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100113
Phil Sutter0af98d32009-02-08 16:44:42 +0100114 return 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100115}
116
Phil Sutter0af98d32009-02-08 16:44:42 +0100117static void rc32434_wdt_ping(void)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100118{
Florian Fainelli03ec5852008-02-25 13:11:31 +0100119 writel(0, &wdt_reg->wtcount);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100120}
121
122static int rc32434_wdt_open(struct inode *inode, struct file *file)
123{
124 if (test_and_set_bit(0, &rc32434_wdt_device.inuse))
125 return -EBUSY;
126
127 if (nowayout)
128 __module_get(THIS_MODULE);
129
Phil Sutter0af98d32009-02-08 16:44:42 +0100130 rc32434_wdt_start();
131 rc32434_wdt_ping();
132
Florian Fainelli03ec5852008-02-25 13:11:31 +0100133 return nonseekable_open(inode, file);
134}
135
136static int rc32434_wdt_release(struct inode *inode, struct file *file)
137{
Phil Sutter0af98d32009-02-08 16:44:42 +0100138 if (expect_close == 42) {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100139 rc32434_wdt_stop();
140 printk(KERN_INFO KBUILD_MODNAME ": disabling watchdog timer\n");
141 module_put(THIS_MODULE);
Phil Sutter0af98d32009-02-08 16:44:42 +0100142 } else {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100143 printk(KERN_CRIT KBUILD_MODNAME
144 ": device closed unexpectedly. WDT will not stop !\n");
Phil Sutter0af98d32009-02-08 16:44:42 +0100145 rc32434_wdt_ping();
146 }
Florian Fainelli03ec5852008-02-25 13:11:31 +0100147 clear_bit(0, &rc32434_wdt_device.inuse);
148 return 0;
149}
150
151static ssize_t rc32434_wdt_write(struct file *file, const char *data,
152 size_t len, loff_t *ppos)
153{
154 if (len) {
155 if (!nowayout) {
156 size_t i;
157
158 /* In case it was set long ago */
159 expect_close = 0;
160
161 for (i = 0; i != len; i++) {
162 char c;
163 if (get_user(c, data + i))
164 return -EFAULT;
165 if (c == 'V')
Phil Sutter0af98d32009-02-08 16:44:42 +0100166 expect_close = 42;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100167 }
168 }
Phil Sutter0af98d32009-02-08 16:44:42 +0100169 rc32434_wdt_ping();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100170 return len;
171 }
172 return 0;
173}
174
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000175static long rc32434_wdt_ioctl(struct file *file, unsigned int cmd,
176 unsigned long arg)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100177{
178 void __user *argp = (void __user *)arg;
179 int new_timeout;
180 unsigned int value;
181 static struct watchdog_info ident = {
182 .options = WDIOF_SETTIMEOUT |
183 WDIOF_KEEPALIVEPING |
184 WDIOF_MAGICCLOSE,
185 .identity = "RC32434_WDT Watchdog",
186 };
187 switch (cmd) {
188 case WDIOC_KEEPALIVE:
Phil Sutter0af98d32009-02-08 16:44:42 +0100189 rc32434_wdt_ping();
Florian Fainelli03ec5852008-02-25 13:11:31 +0100190 break;
191 case WDIOC_GETSTATUS:
192 case WDIOC_GETBOOTSTATUS:
Phil Sutter0af98d32009-02-08 16:44:42 +0100193 value = 0;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100194 if (copy_to_user(argp, &value, sizeof(int)))
195 return -EFAULT;
196 break;
197 case WDIOC_GETSUPPORT:
198 if (copy_to_user(argp, &ident, sizeof(ident)))
199 return -EFAULT;
200 break;
201 case WDIOC_SETOPTIONS:
202 if (copy_from_user(&value, argp, sizeof(int)))
203 return -EFAULT;
204 switch (value) {
205 case WDIOS_ENABLECARD:
206 rc32434_wdt_start();
207 break;
208 case WDIOS_DISABLECARD:
209 rc32434_wdt_stop();
Phil Sutter0af98d32009-02-08 16:44:42 +0100210 break;
Florian Fainelli03ec5852008-02-25 13:11:31 +0100211 default:
212 return -EINVAL;
213 }
214 break;
215 case WDIOC_SETTIMEOUT:
216 if (copy_from_user(&new_timeout, argp, sizeof(int)))
217 return -EFAULT;
Phil Sutter0af98d32009-02-08 16:44:42 +0100218 if (rc32434_wdt_set(new_timeout))
Florian Fainelli03ec5852008-02-25 13:11:31 +0100219 return -EINVAL;
Phil Sutter0af98d32009-02-08 16:44:42 +0100220 /* Fall through */
Florian Fainelli03ec5852008-02-25 13:11:31 +0100221 case WDIOC_GETTIMEOUT:
222 return copy_to_user(argp, &timeout, sizeof(int));
223 default:
224 return -ENOTTY;
225 }
226
227 return 0;
228}
229
230static struct file_operations rc32434_wdt_fops = {
231 .owner = THIS_MODULE,
232 .llseek = no_llseek,
233 .write = rc32434_wdt_write,
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000234 .unlocked_ioctl = rc32434_wdt_ioctl,
Florian Fainelli03ec5852008-02-25 13:11:31 +0100235 .open = rc32434_wdt_open,
236 .release = rc32434_wdt_release,
237};
238
239static struct miscdevice rc32434_wdt_miscdev = {
240 .minor = WATCHDOG_MINOR,
241 .name = "watchdog",
242 .fops = &rc32434_wdt_fops,
243};
244
Phil Sutterd9a87982009-02-08 16:44:42 +0100245static char banner[] __devinitdata = KERN_INFO KBUILD_MODNAME
Florian Fainelli03ec5852008-02-25 13:11:31 +0100246 ": Watchdog Timer version " VERSION ", timer margin: %d sec\n";
247
Phil Sutterd9a87982009-02-08 16:44:42 +0100248static int __devinit rc32434_wdt_probe(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100249{
250 int ret;
251 struct resource *r;
252
Phil Sutter0af98d32009-02-08 16:44:42 +0100253 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rb532_wdt_res");
Florian Fainelli03ec5852008-02-25 13:11:31 +0100254 if (!r) {
255 printk(KERN_ERR KBUILD_MODNAME
256 "failed to retrieve resources\n");
257 return -ENODEV;
258 }
259
260 wdt_reg = ioremap_nocache(r->start, r->end - r->start);
261 if (!wdt_reg) {
262 printk(KERN_ERR KBUILD_MODNAME
263 "failed to remap I/O resources\n");
264 return -ENXIO;
265 }
266
267 ret = misc_register(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100268 if (ret < 0) {
269 printk(KERN_ERR KBUILD_MODNAME
270 "failed to register watchdog device\n");
271 goto unmap;
272 }
273
Florian Fainelli03ec5852008-02-25 13:11:31 +0100274 printk(banner, timeout);
275
276 return 0;
277
278unmap:
279 iounmap(wdt_reg);
280 return ret;
281}
282
Phil Sutterd9a87982009-02-08 16:44:42 +0100283static int __devexit rc32434_wdt_remove(struct platform_device *pdev)
Florian Fainelli03ec5852008-02-25 13:11:31 +0100284{
Florian Fainelli03ec5852008-02-25 13:11:31 +0100285 misc_deregister(&rc32434_wdt_miscdev);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100286 iounmap(wdt_reg);
Florian Fainelli03ec5852008-02-25 13:11:31 +0100287 return 0;
288}
289
290static struct platform_driver rc32434_wdt = {
291 .probe = rc32434_wdt_probe,
Phil Sutterd9a87982009-02-08 16:44:42 +0100292 .remove = __devexit_p(rc32434_wdt_remove),
293 .driver = {
Florian Fainelli03ec5852008-02-25 13:11:31 +0100294 .name = "rc32434_wdt",
295 }
296};
297
298static int __init rc32434_wdt_init(void)
299{
300 return platform_driver_register(&rc32434_wdt);
301}
302
303static void __exit rc32434_wdt_exit(void)
304{
305 platform_driver_unregister(&rc32434_wdt);
306}
307
308module_init(rc32434_wdt_init);
309module_exit(rc32434_wdt_exit);
310
311MODULE_AUTHOR("Ondrej Zajicek <santiago@crfreenet.org>,"
312 "Florian Fainelli <florian@openwrt.org>");
313MODULE_DESCRIPTION("Driver for the IDT RC32434 SoC watchdog");
314MODULE_LICENSE("GPL");
315MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);