blob: b29221783598f7ab2cfc2030d702b6263089a719 [file] [log] [blame]
Matteo Crocec283cf22007-09-20 18:06:41 +02001/*
2 * drivers/watchdog/ar7_wdt.c
3 *
4 * Copyright (C) 2007 Nicolas Thill <nico@openwrt.org>
5 * Copyright (c) 2005 Enrik Berkhan <Enrik.Berkhan@akk.org>
6 *
7 * Some code taken from:
8 * National Semiconductor SCx200 Watchdog support
9 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
10 *
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.
15 *
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.
20 *
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
24 */
25
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>
Florian Fainelli64d40622009-07-21 12:11:32 +020031#include <linux/platform_device.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020032#include <linux/watchdog.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020033#include <linux/fs.h>
34#include <linux/ioport.h>
35#include <linux/io.h>
36#include <linux/uaccess.h>
Florian Fainelli780019d2010-01-27 09:10:06 +010037#include <linux/clk.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020038
39#include <asm/addrspace.h>
Florian Fainellic5e7f5a2009-06-03 13:05:48 +020040#include <asm/mach-ar7/ar7.h>
Matteo Crocec283cf22007-09-20 18:06:41 +020041
42#define DRVNAME "ar7_wdt"
43#define LONGNAME "TI AR7 Watchdog Timer"
44
45MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
46MODULE_DESCRIPTION(LONGNAME);
47MODULE_LICENSE("GPL");
48MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
49
50static int margin = 60;
51module_param(margin, int, 0);
52MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
53
54static int nowayout = WATCHDOG_NOWAYOUT;
55module_param(nowayout, int, 0);
56MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
57
58#define READ_REG(x) readl((void __iomem *)&(x))
59#define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
60
61struct ar7_wdt {
62 u32 kick_lock;
63 u32 kick;
64 u32 change_lock;
65 u32 change;
66 u32 disable_lock;
67 u32 disable;
68 u32 prescale_lock;
69 u32 prescale;
70};
71
Alan Cox670d59c2008-08-04 17:53:22 +010072static unsigned long wdt_is_open;
73static spinlock_t wdt_lock;
Matteo Crocec283cf22007-09-20 18:06:41 +020074static unsigned expect_close;
75
76/* XXX currently fixed, allows max margin ~68.72 secs */
77#define prescale_value 0xffff
78
Florian Fainelli64d40622009-07-21 12:11:32 +020079/* Resource of the WDT registers */
80static struct resource *ar7_regs_wdt;
Matteo Crocec283cf22007-09-20 18:06:41 +020081/* Pointer to the remapped WDT IO space */
82static struct ar7_wdt *ar7_wdt;
Matteo Crocec283cf22007-09-20 18:06:41 +020083
Florian Fainelli780019d2010-01-27 09:10:06 +010084static struct clk *vbus_clk;
85
Matteo Crocec283cf22007-09-20 18:06:41 +020086static void ar7_wdt_kick(u32 value)
87{
88 WRITE_REG(ar7_wdt->kick_lock, 0x5555);
89 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 1) {
90 WRITE_REG(ar7_wdt->kick_lock, 0xaaaa);
91 if ((READ_REG(ar7_wdt->kick_lock) & 3) == 3) {
92 WRITE_REG(ar7_wdt->kick, value);
93 return;
94 }
95 }
96 printk(KERN_ERR DRVNAME ": failed to unlock WDT kick reg\n");
97}
98
99static void ar7_wdt_prescale(u32 value)
100{
101 WRITE_REG(ar7_wdt->prescale_lock, 0x5a5a);
102 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 1) {
103 WRITE_REG(ar7_wdt->prescale_lock, 0xa5a5);
104 if ((READ_REG(ar7_wdt->prescale_lock) & 3) == 3) {
105 WRITE_REG(ar7_wdt->prescale, value);
106 return;
107 }
108 }
109 printk(KERN_ERR DRVNAME ": failed to unlock WDT prescale reg\n");
110}
111
112static void ar7_wdt_change(u32 value)
113{
114 WRITE_REG(ar7_wdt->change_lock, 0x6666);
115 if ((READ_REG(ar7_wdt->change_lock) & 3) == 1) {
116 WRITE_REG(ar7_wdt->change_lock, 0xbbbb);
117 if ((READ_REG(ar7_wdt->change_lock) & 3) == 3) {
118 WRITE_REG(ar7_wdt->change, value);
119 return;
120 }
121 }
122 printk(KERN_ERR DRVNAME ": failed to unlock WDT change reg\n");
123}
124
125static void ar7_wdt_disable(u32 value)
126{
127 WRITE_REG(ar7_wdt->disable_lock, 0x7777);
128 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 1) {
129 WRITE_REG(ar7_wdt->disable_lock, 0xcccc);
130 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 2) {
131 WRITE_REG(ar7_wdt->disable_lock, 0xdddd);
132 if ((READ_REG(ar7_wdt->disable_lock) & 3) == 3) {
133 WRITE_REG(ar7_wdt->disable, value);
134 return;
135 }
136 }
137 }
138 printk(KERN_ERR DRVNAME ": failed to unlock WDT disable reg\n");
139}
140
141static void ar7_wdt_update_margin(int new_margin)
142{
143 u32 change;
Florian Fainelli780019d2010-01-27 09:10:06 +0100144 u32 vbus_rate;
Matteo Crocec283cf22007-09-20 18:06:41 +0200145
Florian Fainelli780019d2010-01-27 09:10:06 +0100146 vbus_rate = clk_get_rate(vbus_clk);
147 change = new_margin * (vbus_rate / prescale_value);
Alan Cox670d59c2008-08-04 17:53:22 +0100148 if (change < 1)
149 change = 1;
150 if (change > 0xffff)
151 change = 0xffff;
Matteo Crocec283cf22007-09-20 18:06:41 +0200152 ar7_wdt_change(change);
Florian Fainelli780019d2010-01-27 09:10:06 +0100153 margin = change * prescale_value / vbus_rate;
Matteo Crocec283cf22007-09-20 18:06:41 +0200154 printk(KERN_INFO DRVNAME
155 ": timer margin %d seconds (prescale %d, change %d, freq %d)\n",
Florian Fainelli780019d2010-01-27 09:10:06 +0100156 margin, prescale_value, change, vbus_rate);
Matteo Crocec283cf22007-09-20 18:06:41 +0200157}
158
159static void ar7_wdt_enable_wdt(void)
160{
161 printk(KERN_DEBUG DRVNAME ": enabling watchdog timer\n");
162 ar7_wdt_disable(1);
163 ar7_wdt_kick(1);
164}
165
166static void ar7_wdt_disable_wdt(void)
167{
168 printk(KERN_DEBUG DRVNAME ": disabling watchdog timer\n");
169 ar7_wdt_disable(0);
170}
171
172static int ar7_wdt_open(struct inode *inode, struct file *file)
173{
174 /* only allow one at a time */
Alan Cox670d59c2008-08-04 17:53:22 +0100175 if (test_and_set_bit(0, &wdt_is_open))
Matteo Crocec283cf22007-09-20 18:06:41 +0200176 return -EBUSY;
177 ar7_wdt_enable_wdt();
178 expect_close = 0;
179
180 return nonseekable_open(inode, file);
181}
182
183static int ar7_wdt_release(struct inode *inode, struct file *file)
184{
185 if (!expect_close)
186 printk(KERN_WARNING DRVNAME
187 ": watchdog device closed unexpectedly,"
188 "will not disable the watchdog timer\n");
189 else if (!nowayout)
190 ar7_wdt_disable_wdt();
Alan Cox670d59c2008-08-04 17:53:22 +0100191 clear_bit(0, &wdt_is_open);
Matteo Crocec283cf22007-09-20 18:06:41 +0200192 return 0;
193}
194
Matteo Crocec283cf22007-09-20 18:06:41 +0200195static ssize_t ar7_wdt_write(struct file *file, const char *data,
196 size_t len, loff_t *ppos)
197{
198 /* check for a magic close character */
199 if (len) {
200 size_t i;
201
Alan Cox670d59c2008-08-04 17:53:22 +0100202 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200203 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100204 spin_unlock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200205
206 expect_close = 0;
207 for (i = 0; i < len; ++i) {
208 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000209 if (get_user(c, data + i))
Matteo Crocec283cf22007-09-20 18:06:41 +0200210 return -EFAULT;
211 if (c == 'V')
212 expect_close = 1;
213 }
214
215 }
216 return len;
217}
218
Alan Cox670d59c2008-08-04 17:53:22 +0100219static long ar7_wdt_ioctl(struct file *file,
220 unsigned int cmd, unsigned long arg)
Matteo Crocec283cf22007-09-20 18:06:41 +0200221{
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000222 static const struct watchdog_info ident = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200223 .identity = LONGNAME,
224 .firmware_version = 1,
Wim Van Sebroecke73a7802009-05-11 18:33:00 +0000225 .options = (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
226 WDIOF_MAGICCLOSE),
Matteo Crocec283cf22007-09-20 18:06:41 +0200227 };
228 int new_margin;
229
230 switch (cmd) {
Matteo Crocec283cf22007-09-20 18:06:41 +0200231 case WDIOC_GETSUPPORT:
232 if (copy_to_user((struct watchdog_info *)arg, &ident,
233 sizeof(ident)))
234 return -EFAULT;
235 return 0;
236 case WDIOC_GETSTATUS:
237 case WDIOC_GETBOOTSTATUS:
238 if (put_user(0, (int *)arg))
239 return -EFAULT;
240 return 0;
241 case WDIOC_KEEPALIVE:
242 ar7_wdt_kick(1);
243 return 0;
244 case WDIOC_SETTIMEOUT:
245 if (get_user(new_margin, (int *)arg))
246 return -EFAULT;
247 if (new_margin < 1)
248 return -EINVAL;
249
Alan Cox670d59c2008-08-04 17:53:22 +0100250 spin_lock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200251 ar7_wdt_update_margin(new_margin);
252 ar7_wdt_kick(1);
Alan Cox670d59c2008-08-04 17:53:22 +0100253 spin_unlock(&wdt_lock);
Matteo Crocec283cf22007-09-20 18:06:41 +0200254
255 case WDIOC_GETTIMEOUT:
256 if (put_user(margin, (int *)arg))
257 return -EFAULT;
258 return 0;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000259 default:
260 return -ENOTTY;
Matteo Crocec283cf22007-09-20 18:06:41 +0200261 }
262}
263
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100264static const struct file_operations ar7_wdt_fops = {
Matteo Crocec283cf22007-09-20 18:06:41 +0200265 .owner = THIS_MODULE,
266 .write = ar7_wdt_write,
Alan Cox670d59c2008-08-04 17:53:22 +0100267 .unlocked_ioctl = ar7_wdt_ioctl,
Matteo Crocec283cf22007-09-20 18:06:41 +0200268 .open = ar7_wdt_open,
269 .release = ar7_wdt_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = no_llseek,
Matteo Crocec283cf22007-09-20 18:06:41 +0200271};
272
273static struct miscdevice ar7_wdt_miscdev = {
274 .minor = WATCHDOG_MINOR,
275 .name = "watchdog",
276 .fops = &ar7_wdt_fops,
277};
278
Florian Fainelli64d40622009-07-21 12:11:32 +0200279static int __devinit ar7_wdt_probe(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200280{
281 int rc;
282
Alan Cox670d59c2008-08-04 17:53:22 +0100283 spin_lock_init(&wdt_lock);
284
Florian Fainelli64d40622009-07-21 12:11:32 +0200285 ar7_regs_wdt =
286 platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
287 if (!ar7_regs_wdt) {
288 printk(KERN_ERR DRVNAME ": could not get registers resource\n");
289 rc = -ENODEV;
290 goto out;
Matteo Crocec283cf22007-09-20 18:06:41 +0200291 }
292
Florian Fainelli64d40622009-07-21 12:11:32 +0200293 if (!request_mem_region(ar7_regs_wdt->start,
294 resource_size(ar7_regs_wdt), LONGNAME)) {
295 printk(KERN_WARNING DRVNAME ": watchdog I/O region busy\n");
296 rc = -EBUSY;
297 goto out;
298 }
299
300 ar7_wdt = ioremap(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
301 if (!ar7_wdt) {
302 printk(KERN_ERR DRVNAME ": could not ioremap registers\n");
303 rc = -ENXIO;
Wim Van Sebroeckd7e97912009-08-31 13:49:14 +0000304 goto out_mem_region;
Florian Fainelli64d40622009-07-21 12:11:32 +0200305 }
Matteo Crocec283cf22007-09-20 18:06:41 +0200306
Florian Fainelli780019d2010-01-27 09:10:06 +0100307 vbus_clk = clk_get(NULL, "vbus");
308 if (IS_ERR(vbus_clk)) {
309 printk(KERN_ERR DRVNAME ": could not get vbus clock\n");
310 rc = PTR_ERR(vbus_clk);
311 goto out_mem_region;
312 }
313
Matteo Crocec283cf22007-09-20 18:06:41 +0200314 ar7_wdt_disable_wdt();
315 ar7_wdt_prescale(prescale_value);
316 ar7_wdt_update_margin(margin);
317
Matteo Crocec283cf22007-09-20 18:06:41 +0200318 rc = misc_register(&ar7_wdt_miscdev);
319 if (rc) {
320 printk(KERN_ERR DRVNAME ": unable to register misc device\n");
Florian Fainelli64d40622009-07-21 12:11:32 +0200321 goto out_alloc;
Matteo Crocec283cf22007-09-20 18:06:41 +0200322 }
323 goto out;
324
Matteo Crocec283cf22007-09-20 18:06:41 +0200325out_alloc:
326 iounmap(ar7_wdt);
Wim Van Sebroeckd7e97912009-08-31 13:49:14 +0000327out_mem_region:
Florian Fainelli64d40622009-07-21 12:11:32 +0200328 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
Matteo Crocec283cf22007-09-20 18:06:41 +0200329out:
330 return rc;
331}
332
Florian Fainelli64d40622009-07-21 12:11:32 +0200333static int __devexit ar7_wdt_remove(struct platform_device *pdev)
Matteo Crocec283cf22007-09-20 18:06:41 +0200334{
335 misc_deregister(&ar7_wdt_miscdev);
Matteo Crocec283cf22007-09-20 18:06:41 +0200336 iounmap(ar7_wdt);
Florian Fainelli64d40622009-07-21 12:11:32 +0200337 release_mem_region(ar7_regs_wdt->start, resource_size(ar7_regs_wdt));
338
339 return 0;
340}
341
342static void ar7_wdt_shutdown(struct platform_device *pdev)
343{
344 if (!nowayout)
345 ar7_wdt_disable_wdt();
346}
347
348static struct platform_driver ar7_wdt_driver = {
349 .probe = ar7_wdt_probe,
350 .remove = __devexit_p(ar7_wdt_remove),
351 .shutdown = ar7_wdt_shutdown,
352 .driver = {
353 .owner = THIS_MODULE,
354 .name = "ar7_wdt",
355 },
356};
357
358static int __init ar7_wdt_init(void)
359{
360 return platform_driver_register(&ar7_wdt_driver);
361}
362
363static void __exit ar7_wdt_cleanup(void)
364{
365 platform_driver_unregister(&ar7_wdt_driver);
Matteo Crocec283cf22007-09-20 18:06:41 +0200366}
367
368module_init(ar7_wdt_init);
369module_exit(ar7_wdt_cleanup);