mm-only debug patch...
[mmotm.git] / drivers / watchdog / ar7_wdt.c
blob2e94b71b20d994e94863a8eb2ede232da4528f6e
1 /*
2 * drivers/watchdog/ar7_wdt.c
4 * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org>
5 * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
7 * Some code taken from:
8 * National Semiconductor SCx200 Watchdog support
9 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/miscdevice.h>
31 #include <linux/platform_device.h>
32 #include <linux/watchdog.h>
33 #include <linux/fs.h>
34 #include <linux/ioport.h>
35 #include <linux/io.h>
36 #include <linux/uaccess.h>
38 #include <asm/addrspace.h>
39 #include <asm/mach-ar7/ar7.h>
41 #define DRVNAME "ar7_wdt"
42 #define LONGNAME "TI AR7 Watchdog Timer"
44 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
45 MODULE_DESCRIPTION(LONGNAME);
46 MODULE_LICENSE("GPL");
47 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
49 static int margin = 60;
50 module_param(margin, int, 0);
51 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
53 static int nowayout = WATCHDOG_NOWAYOUT;
54 module_param(nowayout, int, 0);
55 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
57 #define READ_REG(x) readl((void __iomem *)&(x))
58 #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
60 struct ar7_wdt {
61 u32 kick_lock;
62 u32 kick;
63 u32 change_lock;
64 u32 change;
65 u32 disable_lock;
66 u32 disable;
67 u32 prescale_lock;
68 u32 prescale;
71 static unsigned long wdt_is_open;
72 static spinlock_t wdt_lock;
73 static unsigned expect_close;
75 /* XXX currently fixed, allows max margin ~68.72 secs */
76 #define prescale_value 0xffff
78 /* Resource of the WDT registers */
79 static struct resource *ar7_regs_wdt;
80 /* Pointer to the remapped WDT IO space */
81 static struct ar7_wdt *ar7_wdt;
83 static void ar7_wdt_kick(u32 value)
85 WRITE_REG(ar7_wdt->kick_lock, 0x5555);
86 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
87 WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
88 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
89 WRITE_REG(ar7_wdt->kick, value);
90 return;
93 printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n");
96 static void ar7_wdt_prescale(u32 value)
98 WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
99 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
100 WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
101 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
102 WRITE_REG(ar7_wdt->prescale, value);
103 return;
106 printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n");
109 static void ar7_wdt_change(u32 value)
111 WRITE_REG(ar7_wdt->change_lock, 0x6666);
112 if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
113 WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
114 if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
115 WRITE_REG(ar7_wdt->change, value);
116 return;
119 printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n");
122 static void ar7_wdt_disable(u32 value)
124 WRITE_REG(ar7_wdt->disable_lock, 0x7777);
125 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
126 WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
127 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
128 WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
129 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
130 WRITE_REG(ar7_wdt->disable, value);
131 return;
135 printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n");
138 static void ar7_wdt_update_margin(int new_margin)
140 u32 change;
142 change = new_margin * (ar7_vbus_freq() / prescale_value);
143 if (change < 1)
144 change = 1;
145 if (change > 0xffff)
146 change = 0xffff;
147 ar7_wdt_change(change);
148 margin = change * prescale_value / ar7_vbus_freq();
149 printk(KERN_INFO DRVNAME
150 ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
151 margin, prescale_value, change, ar7_vbus_freq());
154 static void ar7_wdt_enable_wdt(void)
156 printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n");
157 ar7_wdt_disable(1);
158 ar7_wdt_kick(1);
161 static void ar7_wdt_disable_wdt(void)
163 printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n");
164 ar7_wdt_disable(0);
167 static int ar7_wdt_open(struct inode *inode, struct file *file)
169 /* only allow one at a time */
170 if (test_and_set_bit(0, &wdt_is_open))
171 return -EBUSY;
172 ar7_wdt_enable_wdt();
173 expect_close = 0;
175 return nonseekable_open(inode, file);
178 static int ar7_wdt_release(struct inode *inode, struct file *file)
180 if (!expect_close)
181 printk(KERN_WARNING DRVNAME
182 ": watchdog device closed unexpectedly,"
183 "will not disable the watchdog timer\n");
184 else if (!nowayout)
185 ar7_wdt_disable_wdt();
186 clear_bit(0, &wdt_is_open);
187 return 0;
190 static ssize_t ar7_wdt_write(struct file *file, const char *data,
191 size_t len, loff_t *ppos)
193 /* check for a magic close character */
194 if (len) {
195 size_t i;
197 spin_lock(&wdt_lock);
198 ar7_wdt_kick(1);
199 spin_unlock(&wdt_lock);
201 expect_close = 0;
202 for (i = 0; i < len; ++i) {
203 char c;
204 if (get_user(c, data + i))
205 return -EFAULT;
206 if (c == 'V')
207 expect_close = 1;
211 return len;
214 static long ar7_wdt_ioctl(struct file *file,
215 unsigned int cmd, unsigned long arg)
217 static struct watchdog_info ident = {
218 .identity = LONGNAME,
219 .firmware_version = 1,
220 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
221 WDIOF_MAGICCLOSE),
223 int new_margin;
225 switch (cmd) {
226 case WDIOC_GETSUPPORT:
227 if (copy_to_user((struct watchdog_info *)arg, &ident,
228 sizeof(ident)))
229 return -EFAULT;
230 return 0;
231 case WDIOC_GETSTATUS:
232 case WDIOC_GETBOOTSTATUS:
233 if (put_user(0, (int *)arg))
234 return -EFAULT;
235 return 0;
236 case WDIOC_KEEPALIVE:
237 ar7_wdt_kick(1);
238 return 0;
239 case WDIOC_SETTIMEOUT:
240 if (get_user(new_margin, (int *)arg))
241 return -EFAULT;
242 if (new_margin < 1)
243 return -EINVAL;
245 spin_lock(&wdt_lock);
246 ar7_wdt_update_margin(new_margin);
247 ar7_wdt_kick(1);
248 spin_unlock(&wdt_lock);
250 case WDIOC_GETTIMEOUT:
251 if (put_user(margin, (int *)arg))
252 return -EFAULT;
253 return 0;
254 default:
255 return -ENOTTY;
259 static const struct file_operations ar7_wdt_fops = {
260 .owner = THIS_MODULE,
261 .write = ar7_wdt_write,
262 .unlocked_ioctl = ar7_wdt_ioctl,
263 .open = ar7_wdt_open,
264 .release = ar7_wdt_release,
267 static struct miscdevice ar7_wdt_miscdev = {
268 .minor = WATCHDOG_MINOR,
269 .name = "watchdog",
270 .fops = &ar7_wdt_fops,
273 static int __devinit ar7_wdt_probe(struct platform_device *pdev)
275 int rc;
277 spin_lock_init(&wdt_lock);
279 ar7_regs_wdt =
280 platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
281 if (!ar7_regs_wdt) {
282 printk(KERN_ERR DRVNAME ": could not get registers resource\n");
283 rc = -ENODEV;
284 goto out;
287 if (!request_mem_region(ar7_regs_wdt->start,
288 resource_size(ar7_regs_wdt), LONGNAME)) {
289 printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n");
290 rc = -EBUSY;
291 goto out;
294 ar7_wdt = ioremap(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
295 if (!ar7_wdt) {
296 printk(KERN_ERR DRVNAME ": could not ioremap registers\n");
297 rc = -ENXIO;
298 goto out_mem_region;
301 ar7_wdt_disable_wdt();
302 ar7_wdt_prescale(prescale_value);
303 ar7_wdt_update_margin(margin);
305 rc = misc_register(&ar7_wdt_miscdev);
306 if (rc) {
307 printk(KERN_ERR DRVNAME ": unable to register misc device\n");
308 goto out_alloc;
310 goto out;
312 out_alloc:
313 iounmap(ar7_wdt);
314 out_mem_region:
315 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
316 out:
317 return rc;
320 static int __devexit ar7_wdt_remove(struct platform_device *pdev)
322 misc_deregister(&ar7_wdt_miscdev);
323 iounmap(ar7_wdt);
324 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
326 return 0;
329 static void ar7_wdt_shutdown(struct platform_device *pdev)
331 if (!nowayout)
332 ar7_wdt_disable_wdt();
335 static struct platform_driver ar7_wdt_driver = {
336 .probe = ar7_wdt_probe,
337 .remove = __devexit_p(ar7_wdt_remove),
338 .shutdown = ar7_wdt_shutdown,
339 .driver = {
340 .owner = THIS_MODULE,
341 .name = "ar7_wdt",
345 static int __init ar7_wdt_init(void)
347 return platform_driver_register(&ar7_wdt_driver);
350 static void __exit ar7_wdt_cleanup(void)
352 platform_driver_unregister(&ar7_wdt_driver);
355 module_init(ar7_wdt_init);
356 module_exit(ar7_wdt_cleanup);