diff options
author | Domen Puncer <domen.puncer@telargo.com> | 2007-06-18 02:17:57 -0400 |
---|---|---|
committer | Wim Van Sebroeck <wim@iguana.be> | 2007-07-23 12:54:07 -0400 |
commit | 8cf18971ec6ad96cce4a9eb896047581985cf99e (patch) | |
tree | aedd2786f7310c2b9dadbc8831ce7baddd986fd8 /drivers/char/watchdog/mpc5200_wdt.c | |
parent | f695baf2df9e0413d3521661070103711545207a (diff) |
[WATCHDOG] mpc5200 watchdog (GPT0)
Driver for internal mpc5200 watchdog on general purpose timer 0.
For IPB clock of 132 MHz the maximum timeout is about 32 seconds.
Signed-off-by: Domen Puncer <domen.puncer@telargo.com>
Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
Diffstat (limited to 'drivers/char/watchdog/mpc5200_wdt.c')
-rw-r--r-- | drivers/char/watchdog/mpc5200_wdt.c | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/drivers/char/watchdog/mpc5200_wdt.c b/drivers/char/watchdog/mpc5200_wdt.c new file mode 100644 index 000000000000..cc3299c03683 --- /dev/null +++ b/drivers/char/watchdog/mpc5200_wdt.c | |||
@@ -0,0 +1,258 @@ | |||
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 <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 | |||
16 | struct 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 | */ | ||
28 | static struct mpc5200_wdt *wdt_global; | ||
29 | |||
30 | |||
31 | /* helper to calculate timeout in timer counts */ | ||
32 | static 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) */ | ||
41 | static int mpc5200_wdt_get_timeout(struct mpc5200_wdt *wdt) | ||
42 | { | ||
43 | return wdt->count * 0x10000 / wdt->ipb_freq; | ||
44 | } | ||
45 | |||
46 | |||
47 | /* watchdog operations */ | ||
48 | static 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 | } | ||
59 | static 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 | } | ||
65 | static 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 */ | ||
73 | static 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 | } | ||
80 | static struct watchdog_info mpc5200_wdt_info = { | ||
81 | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING, | ||
82 | .identity = "mpc5200 watchdog on GPT0", | ||
83 | }; | ||
84 | static 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 | } | ||
118 | static 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 | } | ||
125 | static 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 | |||
135 | static 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 */ | ||
144 | static 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 | |||
196 | static 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 | } | ||
208 | static 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 | } | ||
214 | static 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 | } | ||
221 | static 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 | |||
228 | static struct of_device_id mpc5200_wdt_match[] = { | ||
229 | { .compatible = "mpc5200-gpt", }, | ||
230 | {}, | ||
231 | }; | ||
232 | static 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 | |||
244 | static int __init mpc5200_wdt_init(void) | ||
245 | { | ||
246 | return of_register_platform_driver(&mpc5200_wdt_driver); | ||
247 | } | ||
248 | |||
249 | static void __exit mpc5200_wdt_exit(void) | ||
250 | { | ||
251 | of_unregister_platform_driver(&mpc5200_wdt_driver); | ||
252 | } | ||
253 | |||
254 | module_init(mpc5200_wdt_init); | ||
255 | module_exit(mpc5200_wdt_exit); | ||
256 | |||
257 | MODULE_AUTHOR("Domen Puncer <domen.puncer@telargo.com>"); | ||
258 | MODULE_LICENSE("Dual BSD/GPL"); | ||