blob: cc3299c0368307eaa319f9d29749408609216b99 [file] [log] [blame]
Domen Puncer8cf18972007-06-18 08:17:57 +02001#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 <asm/of_platform.h>
7#include <asm/uaccess.h>
8#include <asm/mpc52xx.h>
9
10
11#define GPT_MODE_WDT (1<<15)
12#define GPT_MODE_CE (1<<12)
13#define GPT_MODE_MS_TIMER (0x4)
14
15
16struct mpc5200_wdt {
17 unsigned count; /* timer ticks before watchdog kicks in */
18 long ipb_freq;
19 struct miscdevice miscdev;
20 struct resource mem;
21 struct mpc52xx_gpt __iomem *regs;
22};
23
24
25/* misc devices don't provide a way, to get back to 'dev' or 'miscdev' from
26 * file operations, which sucks. But there can be max 1 watchdog anyway, so...
27 */
28static struct mpc5200_wdt *wdt_global;
29
30
31/* helper to calculate timeout in timer counts */
32static void mpc5200_wdt_set_timeout(struct mpc5200_wdt *wdt, int timeout)
33{
34 /* use biggest prescaler of 64k */
35 wdt->count = (wdt->ipb_freq + 0xffff) / 0x10000 * timeout;
36
37 if (wdt->count > 0xffff)
38 wdt->count = 0xffff;
39}
40/* return timeout in seconds (calculated from timer count) */
41static int mpc5200_wdt_get_timeout(struct mpc5200_wdt *wdt)
42{
43 return wdt->count * 0x10000 / wdt->ipb_freq;
44}
45
46
47/* watchdog operations */
48static int mpc5200_wdt_start(struct mpc5200_wdt *wdt)
49{
50 /* disable */
51 out_be32(&wdt->regs->mode, 0);
52 /* set timeout, with maximum prescaler */
53 out_be32(&wdt->regs->count, 0x0 | wdt->count);
54 /* enable watchdog */
55 out_be32(&wdt->regs->mode, GPT_MODE_CE | GPT_MODE_WDT | GPT_MODE_MS_TIMER);
56
57 return 0;
58}
59static int mpc5200_wdt_ping(struct mpc5200_wdt *wdt)
60{
61 /* writing A5 to OCPW resets the watchdog */
62 out_be32(&wdt->regs->mode, 0xA5000000 | (0xffffff & in_be32(&wdt->regs->mode)));
63 return 0;
64}
65static int mpc5200_wdt_stop(struct mpc5200_wdt *wdt)
66{
67 out_be32(&wdt->regs->mode, 0);
68 return 0;
69}
70
71
72/* file operations */
73static ssize_t mpc5200_wdt_write(struct file *file, const char *data,
74 size_t len, loff_t *ppos)
75{
76 struct mpc5200_wdt *wdt = file->private_data;
77 mpc5200_wdt_ping(wdt);
78 return 0;
79}
80static struct watchdog_info mpc5200_wdt_info = {
81 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
82 .identity = "mpc5200 watchdog on GPT0",
83};
84static int mpc5200_wdt_ioctl(struct inode *inode, struct file *file,
85 unsigned int cmd, unsigned long arg)
86{
87 struct mpc5200_wdt *wdt = file->private_data;
88 int __user *data = (int __user *)arg;
89 int timeout;
90 int ret = 0;
91
92 switch (cmd) {
93 case WDIOC_GETSUPPORT:
94 ret = copy_to_user(data, &mpc5200_wdt_info, sizeof(mpc5200_wdt_info));
95 if (ret)
96 ret = -EFAULT;
97 break;
98
99 case WDIOC_KEEPALIVE:
100 mpc5200_wdt_ping(wdt);
101 break;
102
103 case WDIOC_SETTIMEOUT:
104 ret = get_user(timeout, data);
105 if (ret)
106 break;
107 mpc5200_wdt_set_timeout(wdt, timeout);
108 mpc5200_wdt_start(wdt);
109 /* fall through and return the timeout */
110
111 case WDIOC_GETTIMEOUT:
112 timeout = mpc5200_wdt_get_timeout(wdt);
113 ret = put_user(timeout, data);
114 break;
115 }
116 return ret;
117}
118static int mpc5200_wdt_open(struct inode *inode, struct file *file)
119{
120 mpc5200_wdt_set_timeout(wdt_global, 30);
121 mpc5200_wdt_start(wdt_global);
122 file->private_data = wdt_global;
123 return 0;
124}
125static int mpc5200_wdt_release(struct inode *inode, struct file *file)
126{
127#if WATCHDOG_NOWAYOUT == 0
128 struct mpc5200_wdt *wdt = file->private_data;
129 mpc5200_wdt_stop(wdt);
130 wdt->count = 0; /* == disabled */
131#endif
132 return 0;
133}
134
135static struct file_operations mpc5200_wdt_fops = {
136 .owner = THIS_MODULE,
137 .write = mpc5200_wdt_write,
138 .ioctl = mpc5200_wdt_ioctl,
139 .open = mpc5200_wdt_open,
140 .release = mpc5200_wdt_release,
141};
142
143/* module operations */
144static int mpc5200_wdt_probe(struct of_device *op, const struct of_device_id *match)
145{
146 struct mpc5200_wdt *wdt;
147 int err;
148 const void *has_wdt;
149 int size;
150
151 has_wdt = of_get_property(op->node, "has-wdt", NULL);
152 if (!has_wdt)
153 return -ENODEV;
154
155 wdt = kzalloc(sizeof(*wdt), GFP_KERNEL);
156 if (!wdt)
157 return -ENOMEM;
158
159 wdt->ipb_freq = mpc52xx_find_ipb_freq(op->node);
160
161 err = of_address_to_resource(op->node, 0, &wdt->mem);
162 if (err)
163 goto out_free;
164 size = wdt->mem.end - wdt->mem.start + 1;
165 if (!request_mem_region(wdt->mem.start, size, "mpc5200_wdt")) {
166 err = -ENODEV;
167 goto out_free;
168 }
169 wdt->regs = ioremap(wdt->mem.start, size);
170 if (!wdt->regs) {
171 err = -ENODEV;
172 goto out_release;
173 }
174
175 dev_set_drvdata(&op->dev, wdt);
176
177 wdt->miscdev = (struct miscdevice) {
178 .minor = WATCHDOG_MINOR,
179 .name = "watchdog",
180 .fops = &mpc5200_wdt_fops,
181 .parent = &op->dev,
182 };
183 wdt_global = wdt;
184 err = misc_register(&wdt->miscdev);
185 if (!err)
186 return 0;
187
188 iounmap(wdt->regs);
189 out_release:
190 release_mem_region(wdt->mem.start, size);
191 out_free:
192 kfree(wdt);
193 return err;
194}
195
196static int mpc5200_wdt_remove(struct of_device *op)
197{
198 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
199
200 mpc5200_wdt_stop(wdt);
201 misc_deregister(&wdt->miscdev);
202 iounmap(wdt->regs);
203 release_mem_region(wdt->mem.start, wdt->mem.end - wdt->mem.start + 1);
204 kfree(wdt);
205
206 return 0;
207}
208static int mpc5200_wdt_suspend(struct of_device *op, pm_message_t state)
209{
210 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
211 mpc5200_wdt_stop(wdt);
212 return 0;
213}
214static int mpc5200_wdt_resume(struct of_device *op)
215{
216 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
217 if (wdt->count)
218 mpc5200_wdt_start(wdt);
219 return 0;
220}
221static int mpc5200_wdt_shutdown(struct of_device *op)
222{
223 struct mpc5200_wdt *wdt = dev_get_drvdata(&op->dev);
224 mpc5200_wdt_stop(wdt);
225 return 0;
226}
227
228static struct of_device_id mpc5200_wdt_match[] = {
229 { .compatible = "mpc5200-gpt", },
230 {},
231};
232static struct of_platform_driver mpc5200_wdt_driver = {
233 .owner = THIS_MODULE,
234 .name = "mpc5200-gpt-wdt",
235 .match_table = mpc5200_wdt_match,
236 .probe = mpc5200_wdt_probe,
237 .remove = mpc5200_wdt_remove,
238 .suspend = mpc5200_wdt_suspend,
239 .resume = mpc5200_wdt_resume,
240 .shutdown = mpc5200_wdt_shutdown,
241};
242
243
244static int __init mpc5200_wdt_init(void)
245{
246 return of_register_platform_driver(&mpc5200_wdt_driver);
247}
248
249static void __exit mpc5200_wdt_exit(void)
250{
251 of_unregister_platform_driver(&mpc5200_wdt_driver);
252}
253
254module_init(mpc5200_wdt_init);
255module_exit(mpc5200_wdt_exit);
256
257MODULE_AUTHOR("Domen Puncer <domen.puncer@telargo.com>");
258MODULE_LICENSE("Dual BSD/GPL");