[MAC80211]: Update beacon_update callback documentation
[linux-2.6/kmemtrace.git] / drivers / char / watchdog / mpc5200_wdt.c
blob564143d406105dfc5bce3bee89f6cd539ead4b20
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/miscdevice.h>
4 #include <linux/watchdog.h>
5 #include <linux/io.h>
6 #include <linux/spinlock.h>
7 #include <asm/of_platform.h>
8 #include <asm/uaccess.h>
9 #include <asm/mpc52xx.h>
12 #define GPT_MODE_WDT (1<<15)
13 #define GPT_MODE_CE (1<<12)
14 #define GPT_MODE_MS_TIMER (0x4)
17 struct mpc5200_wdt {
18 unsigned count; /* timer ticks before watchdog kicks in */
19 long ipb_freq;
20 struct miscdevice miscdev;
21 struct resource mem;
22 struct mpc52xx_gpt __iomem *regs;
23 spinlock_t io_lock;
26 /* is_active stores wether or not the /dev/watchdog device is opened */
27 static unsigned long is_active;
29 /* misc devices don't provide a way, to get back to 'dev' or 'miscdev' from
30 * file operations, which sucks. But there can be max 1 watchdog anyway, so...
32 static struct mpc5200_wdt *wdt_global;
35 /* helper to calculate timeout in timer counts */
36 static void mpc5200_wdt_set_timeout(struct mpc5200_wdt *wdt, int timeout)
38 /* use biggest prescaler of 64k */
39 wdt->count = (wdt->ipb_freq + 0xffff) / 0x10000 * timeout;
41 if (wdt->count > 0xffff)
42 wdt->count = 0xffff;
44 /* return timeout in seconds (calculated from timer count) */
45 static int mpc5200_wdt_get_timeout(struct mpc5200_wdt *wdt)
47 return wdt->count * 0x10000 / wdt->ipb_freq;
51 /* watchdog operations */
52 static int mpc5200_wdt_start(struct mpc5200_wdt *wdt)
54 spin_lock(&wdt->io_lock);
55 /* disable */
56 out_be32(&wdt->regs->mode, 0);
57 /* set timeout, with maximum prescaler */
58 out_be32(&wdt->regs->count, 0x0 | wdt->count);
59 /* enable watchdog */
60 out_be32(&wdt->regs->mode, GPT_MODE_CE | GPT_MODE_WDT | GPT_MODE_MS_TIMER);
61 spin_unlock(&wdt->io_lock);
63 return 0;
65 static int mpc5200_wdt_ping(struct mpc5200_wdt *wdt)
67 spin_lock(&wdt->io_lock);
68 /* writing A5 to OCPW resets the watchdog */
69 out_be32(&wdt->regs->mode, 0xA5000000 | (0xffffff & in_be32(&wdt->regs->mode)));
70 spin_unlock(&wdt->io_lock);
71 return 0;
73 static int mpc5200_wdt_stop(struct mpc5200_wdt *wdt)
75 spin_lock(&wdt->io_lock);
76 /* disable */
77 out_be32(&wdt->regs->mode, 0);
78 spin_unlock(&wdt->io_lock);
79 return 0;
83 /* file operations */
84 static ssize_t mpc5200_wdt_write(struct file *file, const char *data,
85 size_t len, loff_t *ppos)
87 struct mpc5200_wdt *wdt = file->private_data;
88 mpc5200_wdt_ping(wdt);
89 return 0;
91 static struct watchdog_info mpc5200_wdt_info = {
92 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
93 .identity = "mpc5200 watchdog on GPT0",
95 static int mpc5200_wdt_ioctl(struct inode *inode, struct file *file,
96 unsigned int cmd, unsigned long arg)
98 struct mpc5200_wdt *wdt = file->private_data;
99 int __user *data = (int __user *)arg;
100 int timeout;
101 int ret = 0;
103 switch (cmd) {
104 case WDIOC_GETSUPPORT:
105 ret = copy_to_user(data, &mpc5200_wdt_info,
106 sizeof(mpc5200_wdt_info));
107 if (ret)
108 ret = -EFAULT;
109 break;
111 case WDIOC_GETSTATUS:
112 case WDIOC_GETBOOTSTATUS:
113 ret = put_user(0, data);
114 break;
116 case WDIOC_KEEPALIVE:
117 mpc5200_wdt_ping(wdt);
118 break;
120 case WDIOC_SETTIMEOUT:
121 ret = get_user(timeout, data);
122 if (ret)
123 break;
124 mpc5200_wdt_set_timeout(wdt, timeout);
125 mpc5200_wdt_start(wdt);
126 /* fall through and return the timeout */
128 case WDIOC_GETTIMEOUT:
129 timeout = mpc5200_wdt_get_timeout(wdt);
130 ret = put_user(timeout, data);
131 break;
133 default:
134 ret = -ENOTTY;
136 return ret;
138 static int mpc5200_wdt_open(struct inode *inode, struct file *file)
140 /* /dev/watchdog can only be opened once */
141 if (test_and_set_bit(0, &is_active))
142 return -EBUSY;
144 /* Set and activate the watchdog */
145 mpc5200_wdt_set_timeout(wdt_global, 30);
146 mpc5200_wdt_start(wdt_global);
147 file->private_data = wdt_global;
148 return nonseekable_open(inode, file);
150 static int mpc5200_wdt_release(struct inode *inode, struct file *file)
152 #if WATCHDOG_NOWAYOUT == 0
153 struct mpc5200_wdt *wdt = file->private_data;
154 mpc5200_wdt_stop(wdt);
155 wdt->count = 0; /* == disabled */
156 #endif
157 clear_bit(0, &is_active);
158 return 0;
161 static struct file_operations mpc5200_wdt_fops = {
162 .owner = THIS_MODULE,
163 .write = mpc5200_wdt_write,
164 .ioctl = mpc5200_wdt_ioctl,
165 .open = mpc5200_wdt_open,
166 .release = mpc5200_wdt_release,
169 /* module operations */
170 static int mpc5200_wdt_probe(struct of_device *op, const struct of_device_id *match)
172 struct mpc5200_wdt *wdt;
173 int err;
174 const void *has_wdt;
175 int size;
177 has_wdt = of_get_property(op->node, "has-wdt", NULL);
178 if (!has_wdt)
179 return -ENODEV;
181 wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
182 if (!wdt)
183 return -ENOMEM;
185 wdt->ipb_freq = mpc52xx_find_ipb_freq(op->node);
187 err = of_address_to_resource(op->node, 0, &wdt->mem);
188 if (err)
189 goto out_free;
190 size = wdt->mem.end - wdt->mem.start + 1;
191 if (!request_mem_region(wdt->mem.start, size, "mpc5200_wdt")) {
192 err = -ENODEV;
193 goto out_free;
195 wdt->regs = ioremap(wdt->mem.start, size);
196 if (!wdt->regs) {
197 err = -ENODEV;
198 goto out_release;
201 dev_set_drvdata(&op->dev, wdt);
202 spin_lock_init(&wdt->io_lock);
204 wdt->miscdev = (struct miscdevice) {
205 .minor = WATCHDOG_MINOR,
206 .name = "watchdog",
207 .fops = &mpc5200_wdt_fops,
208 .parent = &op->dev,
210 wdt_global = wdt;
211 err = misc_register(&wdt->miscdev);
212 if (!err)
213 return 0;
215 iounmap(wdt->regs);
216 out_release:
217 release_mem_region(wdt->mem.start, size);
218 out_free:
219 kfree(wdt);
220 return err;
223 static int mpc5200_wdt_remove(struct of_device *op)
225 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
227 mpc5200_wdt_stop(wdt);
228 misc_deregister(&wdt->miscdev);
229 iounmap(wdt->regs);
230 release_mem_region(wdt->mem.start, wdt->mem.end - wdt->mem.start + 1);
231 kfree(wdt);
233 return 0;
235 static int mpc5200_wdt_suspend(struct of_device *op, pm_message_t state)
237 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
238 mpc5200_wdt_stop(wdt);
239 return 0;
241 static int mpc5200_wdt_resume(struct of_device *op)
243 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
244 if (wdt->count)
245 mpc5200_wdt_start(wdt);
246 return 0;
248 static int mpc5200_wdt_shutdown(struct of_device *op)
250 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
251 mpc5200_wdt_stop(wdt);
252 return 0;
255 static struct of_device_id mpc5200_wdt_match[] = {
256 { .compatible = "mpc5200-gpt", },
259 static struct of_platform_driver mpc5200_wdt_driver = {
260 .owner = THIS_MODULE,
261 .name = "mpc5200-gpt-wdt",
262 .match_table = mpc5200_wdt_match,
263 .probe = mpc5200_wdt_probe,
264 .remove = mpc5200_wdt_remove,
265 .suspend = mpc5200_wdt_suspend,
266 .resume = mpc5200_wdt_resume,
267 .shutdown = mpc5200_wdt_shutdown,
271 static int __init mpc5200_wdt_init(void)
273 return of_register_platform_driver(&mpc5200_wdt_driver);
276 static void __exit mpc5200_wdt_exit(void)
278 of_unregister_platform_driver(&mpc5200_wdt_driver);
281 module_init(mpc5200_wdt_init);
282 module_exit(mpc5200_wdt_exit);
284 MODULE_AUTHOR("Domen Puncer <domen.puncer@telargo.com>");
285 MODULE_LICENSE("Dual BSD/GPL");
286 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);