]> err.no Git - linux-2.6/blob - drivers/watchdog/mpc83xx_wdt.c
109eea0df2d041be01032b96110040092594001f
[linux-2.6] / drivers / watchdog / mpc83xx_wdt.c
1 /*
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
18 #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>
25 #include <linux/io.h>
26 #include <linux/uaccess.h>
27
28 struct 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
41 static struct mpc83xx_wdt __iomem *wd_base;
42
43 static u16 timeout = 0xffff;
44 module_param(timeout, ushort, 0);
45 MODULE_PARM_DESC(timeout,
46         "Watchdog timeout in ticks. (0<timeout<65536, default=65535");
47
48 static int reset = 1;
49 module_param(reset, bool, 0);
50 MODULE_PARM_DESC(reset,
51         "Watchdog Interrupt/Reset Mode. 0 = interrupt, 1 = reset");
52
53 /*
54  * We always prescale, but if someone really doesn't want to they can set this
55  * to 0
56  */
57 static int prescale = 1;
58 static unsigned int timeout_sec;
59
60 static unsigned long wdt_is_open;
61 static DEFINE_SPINLOCK(wdt_spinlock);
62
63 static 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
72 static 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
80 static 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
102 static 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
110 static long mpc83xx_wdt_ioctl(struct file *file, unsigned int cmd,
111                                                         unsigned long arg)
112 {
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;
124         case WDIOC_GETSTATUS:
125         case WDIOC_GETBOOTSTATUS:
126                 return put_user(0, p);
127         case WDIOC_KEEPALIVE:
128                 mpc83xx_wdt_keepalive();
129                 return 0;
130         case WDIOC_GETTIMEOUT:
131                 return put_user(timeout_sec, p);
132         default:
133                 return -ENOTTY;
134         }
135 }
136
137 static const struct file_operations mpc83xx_wdt_fops = {
138         .owner          = THIS_MODULE,
139         .llseek         = no_llseek,
140         .write          = mpc83xx_wdt_write,
141         .unlocked_ioctl = mpc83xx_wdt_ioctl,
142         .open           = mpc83xx_wdt_open,
143         .release        = mpc83xx_wdt_release,
144 };
145
146 static struct miscdevice mpc83xx_wdt_miscdev = {
147         .minor  = WATCHDOG_MINOR,
148         .name   = "watchdog",
149         .fops   = &mpc83xx_wdt_fops,
150 };
151
152 static 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
166         wd_base = ioremap(r->start, sizeof(struct mpc83xx_wdt));
167         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);
189         return 0;
190
191 err_unmap:
192         iounmap(wd_base);
193 err_out:
194         return ret;
195 }
196
197 static 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
205 static struct platform_driver mpc83xx_wdt_driver = {
206         .probe          = mpc83xx_wdt_probe,
207         .remove         = __devexit_p(mpc83xx_wdt_remove),
208         .driver         = {
209                 .name   = "mpc83xx_wdt",
210                 .owner  = THIS_MODULE,
211         },
212 };
213
214 static int __init mpc83xx_wdt_init(void)
215 {
216         return platform_driver_register(&mpc83xx_wdt_driver);
217 }
218
219 static void __exit mpc83xx_wdt_exit(void)
220 {
221         platform_driver_unregister(&mpc83xx_wdt_driver);
222 }
223
224 module_init(mpc83xx_wdt_init);
225 module_exit(mpc83xx_wdt_exit);
226
227 MODULE_AUTHOR("Dave Updegraff, Kumar Gala");
228 MODULE_DESCRIPTION("Driver for watchdog timer in MPC83xx uProcessor");
229 MODULE_LICENSE("GPL");
230 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
231 MODULE_ALIAS("platform:mpc83xx_wdt");