blob: 109eea0df2d041be01032b96110040092594001f [file] [log] [blame]
Kumar Galafabbfb92006-01-14 13:20:50 -08001/*
2 * mpc83xx_wdt.c - MPC83xx watchdog userspace interface
3 *
4 * Authors: Dave Updegraff <dave@cray.org>
5 * Kumar Gala <galak@kernel.crashing.org>
6 * Attribution: from 83xx_wst: Florian Schirmer <jolt@tuxbox.org>
7 * ..and from sc520_wdt
8 *
9 * Note: it appears that you can only actually ENABLE or DISABLE the thing
10 * once after POR. Once enabled, you cannot disable, and vice versa.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 */
17
Kumar Galafabbfb92006-01-14 13:20:50 -080018#include <linux/fs.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
21#include <linux/miscdevice.h>
22#include <linux/platform_device.h>
23#include <linux/module.h>
24#include <linux/watchdog.h>
Alan Coxf26ef3d2008-05-19 14:07:09 +010025#include <linux/io.h>
26#include <linux/uaccess.h>
Kumar Galafabbfb92006-01-14 13:20:50 -080027
28struct mpc83xx_wdt {
29 __be32 res0;
30 __be32 swcrr; /* System watchdog control register */
31#define SWCRR_SWTC 0xFFFF0000 /* Software Watchdog Time Count. */
32#define SWCRR_SWEN 0x00000004 /* Watchdog Enable bit. */
33#define SWCRR_SWRI 0x00000002 /* Software Watchdog Reset/Interrupt Select bit.*/
34#define SWCRR_SWPR 0x00000001 /* Software Watchdog Counter Prescale bit. */
35 __be32 swcnr; /* System watchdog count register */
36 u8 res1[2];
37 __be16 swsrr; /* System watchdog service register */
38 u8 res2[0xF0];
39};
40
41static struct mpc83xx_wdt __iomem *wd_base;
42
43static u16 timeout = 0xffff;
44module_param(timeout, ushort, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010045MODULE_PARM_DESC(timeout,
46 "Watchdog timeout in ticks. (0<timeout<65536, default=65535");
Kumar Galafabbfb92006-01-14 13:20:50 -080047
48static int reset = 1;
49module_param(reset, bool, 0);
Alan Coxf26ef3d2008-05-19 14:07:09 +010050MODULE_PARM_DESC(reset,
51 "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
Kumar Galafabbfb92006-01-14 13:20:50 -080052
53/*
54 * We always prescale, but if someone really doesn't want to they can set this
55 * to 0
56 */
57static int prescale = 1;
58static unsigned int timeout_sec;
59
60static unsigned long wdt_is_open;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070061static DEFINE_SPINLOCK(wdt_spinlock);
Kumar Galafabbfb92006-01-14 13:20:50 -080062
63static void mpc83xx_wdt_keepalive(void)
64{
65 /* Ping the WDT */
66 spin_lock(&wdt_spinlock);
67 out_be16(&wd_base->swsrr, 0x556c);
68 out_be16(&wd_base->swsrr, 0xaa39);
69 spin_unlock(&wdt_spinlock);
70}
71
72static ssize_t mpc83xx_wdt_write(struct file *file, const char __user *buf,
73 size_t count, loff_t *ppos)
74{
75 if (count)
76 mpc83xx_wdt_keepalive();
77 return count;
78}
79
80static int mpc83xx_wdt_open(struct inode *inode, struct file *file)
81{
82 u32 tmp = SWCRR_SWEN;
83 if (test_and_set_bit(0, &wdt_is_open))
84 return -EBUSY;
85
86 /* Once we start the watchdog we can't stop it */
87 __module_get(THIS_MODULE);
88
89 /* Good, fire up the show */
90 if (prescale)
91 tmp |= SWCRR_SWPR;
92 if (reset)
93 tmp |= SWCRR_SWRI;
94
95 tmp |= timeout << 16;
96
97 out_be32(&wd_base->swcrr, tmp);
98
99 return nonseekable_open(inode, file);
100}
101
102static int mpc83xx_wdt_release(struct inode *inode, struct file *file)
103{
104 printk(KERN_CRIT "Unexpected close, not stopping watchdog!\n");
105 mpc83xx_wdt_keepalive();
106 clear_bit(0, &wdt_is_open);
107 return 0;
108}
109
Alan Coxf26ef3d2008-05-19 14:07:09 +0100110static long mpc83xx_wdt_ioctl(struct file *file, unsigned int cmd,
111 unsigned long arg)
Kumar Galafabbfb92006-01-14 13:20:50 -0800112{
113 void __user *argp = (void __user *)arg;
114 int __user *p = argp;
115 static struct watchdog_info ident = {
116 .options = WDIOF_KEEPALIVEPING,
117 .firmware_version = 1,
118 .identity = "MPC83xx",
119 };
120
121 switch (cmd) {
122 case WDIOC_GETSUPPORT:
123 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Wim Van Sebroeck5c4eb612007-07-21 13:42:18 +0000124 case WDIOC_GETSTATUS:
125 case WDIOC_GETBOOTSTATUS:
126 return put_user(0, p);
Kumar Galafabbfb92006-01-14 13:20:50 -0800127 case WDIOC_KEEPALIVE:
128 mpc83xx_wdt_keepalive();
129 return 0;
130 case WDIOC_GETTIMEOUT:
131 return put_user(timeout_sec, p);
132 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200133 return -ENOTTY;
Kumar Galafabbfb92006-01-14 13:20:50 -0800134 }
135}
136
Arjan van de Ven62322d22006-07-03 00:24:21 -0700137static const struct file_operations mpc83xx_wdt_fops = {
Kumar Galafabbfb92006-01-14 13:20:50 -0800138 .owner = THIS_MODULE,
139 .llseek = no_llseek,
140 .write = mpc83xx_wdt_write,
Alan Coxf26ef3d2008-05-19 14:07:09 +0100141 .unlocked_ioctl = mpc83xx_wdt_ioctl,
Kumar Galafabbfb92006-01-14 13:20:50 -0800142 .open = mpc83xx_wdt_open,
143 .release = mpc83xx_wdt_release,
144};
145
146static struct miscdevice mpc83xx_wdt_miscdev = {
147 .minor = WATCHDOG_MINOR,
148 .name = "watchdog",
149 .fops = &mpc83xx_wdt_fops,
150};
151
152static int __devinit mpc83xx_wdt_probe(struct platform_device *dev)
153{
154 struct resource *r;
155 int ret;
156 unsigned int *freq = dev->dev.platform_data;
157
158 /* get a pointer to the register memory */
159 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
160
161 if (!r) {
162 ret = -ENODEV;
163 goto err_out;
164 }
165
Alan Coxf26ef3d2008-05-19 14:07:09 +0100166 wd_base = ioremap(r->start, sizeof(struct mpc83xx_wdt));
Kumar Galafabbfb92006-01-14 13:20:50 -0800167 if (wd_base == NULL) {
168 ret = -ENOMEM;
169 goto err_out;
170 }
171
172 ret = misc_register(&mpc83xx_wdt_miscdev);
173 if (ret) {
174 printk(KERN_ERR "cannot register miscdev on minor=%d "
175 "(err=%d)\n",
176 WATCHDOG_MINOR, ret);
177 goto err_unmap;
178 }
179
180 /* Calculate the timeout in seconds */
181 if (prescale)
182 timeout_sec = (timeout * 0x10000) / (*freq);
183 else
184 timeout_sec = timeout / (*freq);
185
186 printk(KERN_INFO "WDT driver for MPC83xx initialized. "
187 "mode:%s timeout=%d (%d seconds)\n",
188 reset ? "reset":"interrupt", timeout, timeout_sec);
Kumar Galafabbfb92006-01-14 13:20:50 -0800189 return 0;
190
191err_unmap:
192 iounmap(wd_base);
193err_out:
194 return ret;
195}
196
197static int __devexit mpc83xx_wdt_remove(struct platform_device *dev)
198{
199 misc_deregister(&mpc83xx_wdt_miscdev);
200 iounmap(wd_base);
201
202 return 0;
203}
204
205static struct platform_driver mpc83xx_wdt_driver = {
206 .probe = mpc83xx_wdt_probe,
207 .remove = __devexit_p(mpc83xx_wdt_remove),
208 .driver = {
209 .name = "mpc83xx_wdt",
Kay Sieversf37d1932008-04-10 21:29:23 -0700210 .owner = THIS_MODULE,
Kumar Galafabbfb92006-01-14 13:20:50 -0800211 },
212};
213
214static int __init mpc83xx_wdt_init(void)
215{
216 return platform_driver_register(&mpc83xx_wdt_driver);
217}
218
219static void __exit mpc83xx_wdt_exit(void)
220{
221 platform_driver_unregister(&mpc83xx_wdt_driver);
222}
223
224module_init(mpc83xx_wdt_init);
225module_exit(mpc83xx_wdt_exit);
226
227MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
228MODULE_DESCRIPTION("Driver for watchdog timer in MPC83xx uProcessor");
229MODULE_LICENSE("GPL");
230MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
Kay Sieversf37d1932008-04-10 21:29:23 -0700231MODULE_ALIAS("platform:mpc83xx_wdt");