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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/miscdevice.h>
33 #include <linux/platform_device.h>
34 #include <linux/watchdog.h>
36 #include <linux/ioport.h>
38 #include <linux/uaccess.h>
39 #include <linux/clk.h>
41 #include <asm/addrspace.h>
42 #include <asm/mach-ar7/ar7.h>
44 #define LONGNAME "TI AR7 Watchdog Timer"
46 MODULE_AUTHOR("Nicolas Thill <nico@openwrt.org>");
47 MODULE_DESCRIPTION(LONGNAME
);
48 MODULE_LICENSE("GPL");
49 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
51 static int margin
= 60;
52 module_param(margin
, int, 0);
53 MODULE_PARM_DESC(margin
, "Watchdog margin in seconds");
55 static bool nowayout
= WATCHDOG_NOWAYOUT
;
56 module_param(nowayout
, bool, 0);
57 MODULE_PARM_DESC(nowayout
, "Disable watchdog shutdown on close");
59 #define READ_REG(x) readl((void __iomem *)&(x))
60 #define WRITE_REG(x, v) writel((v), (void __iomem *)&(x))
73 static unsigned long wdt_is_open
;
74 static unsigned expect_close
;
75 static DEFINE_SPINLOCK(wdt_lock
);
77 /* XXX currently fixed, allows max margin ~68.72 secs */
78 #define prescale_value 0xffff
80 /* Resource of the WDT registers */
81 static struct resource
*ar7_regs_wdt
;
82 /* Pointer to the remapped WDT IO space */
83 static struct ar7_wdt
*ar7_wdt
;
85 static struct clk
*vbus_clk
;
87 static void ar7_wdt_kick(u32 value
)
89 WRITE_REG(ar7_wdt
->kick_lock
, 0x5555);
90 if ((READ_REG(ar7_wdt
->kick_lock
) & 3) == 1) {
91 WRITE_REG(ar7_wdt
->kick_lock
, 0xaaaa);
92 if ((READ_REG(ar7_wdt
->kick_lock
) & 3) == 3) {
93 WRITE_REG(ar7_wdt
->kick
, value
);
97 pr_err("failed to unlock WDT kick reg\n");
100 static void ar7_wdt_prescale(u32 value
)
102 WRITE_REG(ar7_wdt
->prescale_lock
, 0x5a5a);
103 if ((READ_REG(ar7_wdt
->prescale_lock
) & 3) == 1) {
104 WRITE_REG(ar7_wdt
->prescale_lock
, 0xa5a5);
105 if ((READ_REG(ar7_wdt
->prescale_lock
) & 3) == 3) {
106 WRITE_REG(ar7_wdt
->prescale
, value
);
110 pr_err("failed to unlock WDT prescale reg\n");
113 static void ar7_wdt_change(u32 value
)
115 WRITE_REG(ar7_wdt
->change_lock
, 0x6666);
116 if ((READ_REG(ar7_wdt
->change_lock
) & 3) == 1) {
117 WRITE_REG(ar7_wdt
->change_lock
, 0xbbbb);
118 if ((READ_REG(ar7_wdt
->change_lock
) & 3) == 3) {
119 WRITE_REG(ar7_wdt
->change
, value
);
123 pr_err("failed to unlock WDT change reg\n");
126 static void ar7_wdt_disable(u32 value
)
128 WRITE_REG(ar7_wdt
->disable_lock
, 0x7777);
129 if ((READ_REG(ar7_wdt
->disable_lock
) & 3) == 1) {
130 WRITE_REG(ar7_wdt
->disable_lock
, 0xcccc);
131 if ((READ_REG(ar7_wdt
->disable_lock
) & 3) == 2) {
132 WRITE_REG(ar7_wdt
->disable_lock
, 0xdddd);
133 if ((READ_REG(ar7_wdt
->disable_lock
) & 3) == 3) {
134 WRITE_REG(ar7_wdt
->disable
, value
);
139 pr_err("failed to unlock WDT disable reg\n");
142 static void ar7_wdt_update_margin(int new_margin
)
147 vbus_rate
= clk_get_rate(vbus_clk
);
148 change
= new_margin
* (vbus_rate
/ prescale_value
);
153 ar7_wdt_change(change
);
154 margin
= change
* prescale_value
/ vbus_rate
;
155 pr_info("timer margin %d seconds (prescale %d, change %d, freq %d)\n",
156 margin
, prescale_value
, change
, vbus_rate
);
159 static void ar7_wdt_enable_wdt(void)
161 pr_debug("enabling watchdog timer\n");
166 static void ar7_wdt_disable_wdt(void)
168 pr_debug("disabling watchdog timer\n");
172 static int ar7_wdt_open(struct inode
*inode
, struct file
*file
)
174 /* only allow one at a time */
175 if (test_and_set_bit(0, &wdt_is_open
))
177 ar7_wdt_enable_wdt();
180 return nonseekable_open(inode
, file
);
183 static int ar7_wdt_release(struct inode
*inode
, struct file
*file
)
186 pr_warn("watchdog device closed unexpectedly, will not disable the watchdog timer\n");
188 ar7_wdt_disable_wdt();
189 clear_bit(0, &wdt_is_open
);
193 static ssize_t
ar7_wdt_write(struct file
*file
, const char *data
,
194 size_t len
, loff_t
*ppos
)
196 /* check for a magic close character */
200 spin_lock(&wdt_lock
);
202 spin_unlock(&wdt_lock
);
205 for (i
= 0; i
< len
; ++i
) {
207 if (get_user(c
, data
+ i
))
217 static long ar7_wdt_ioctl(struct file
*file
,
218 unsigned int cmd
, unsigned long arg
)
220 static const struct watchdog_info ident
= {
221 .identity
= LONGNAME
,
222 .firmware_version
= 1,
223 .options
= (WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
|
229 case WDIOC_GETSUPPORT
:
230 if (copy_to_user((struct watchdog_info
*)arg
, &ident
,
234 case WDIOC_GETSTATUS
:
235 case WDIOC_GETBOOTSTATUS
:
236 if (put_user(0, (int *)arg
))
239 case WDIOC_KEEPALIVE
:
242 case WDIOC_SETTIMEOUT
:
243 if (get_user(new_margin
, (int *)arg
))
248 spin_lock(&wdt_lock
);
249 ar7_wdt_update_margin(new_margin
);
251 spin_unlock(&wdt_lock
);
253 case WDIOC_GETTIMEOUT
:
254 if (put_user(margin
, (int *)arg
))
262 static const struct file_operations ar7_wdt_fops
= {
263 .owner
= THIS_MODULE
,
264 .write
= ar7_wdt_write
,
265 .unlocked_ioctl
= ar7_wdt_ioctl
,
266 .open
= ar7_wdt_open
,
267 .release
= ar7_wdt_release
,
271 static struct miscdevice ar7_wdt_miscdev
= {
272 .minor
= WATCHDOG_MINOR
,
274 .fops
= &ar7_wdt_fops
,
277 static int __devinit
ar7_wdt_probe(struct platform_device
*pdev
)
282 platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
284 pr_err("could not get registers resource\n");
289 if (!request_mem_region(ar7_regs_wdt
->start
,
290 resource_size(ar7_regs_wdt
), LONGNAME
)) {
291 pr_warn("watchdog I/O region busy\n");
296 ar7_wdt
= ioremap(ar7_regs_wdt
->start
, resource_size(ar7_regs_wdt
));
298 pr_err("could not ioremap registers\n");
303 vbus_clk
= clk_get(NULL
, "vbus");
304 if (IS_ERR(vbus_clk
)) {
305 pr_err("could not get vbus clock\n");
306 rc
= PTR_ERR(vbus_clk
);
310 ar7_wdt_disable_wdt();
311 ar7_wdt_prescale(prescale_value
);
312 ar7_wdt_update_margin(margin
);
314 rc
= misc_register(&ar7_wdt_miscdev
);
316 pr_err("unable to register misc device\n");
324 release_mem_region(ar7_regs_wdt
->start
, resource_size(ar7_regs_wdt
));
329 static int __devexit
ar7_wdt_remove(struct platform_device
*pdev
)
331 misc_deregister(&ar7_wdt_miscdev
);
333 release_mem_region(ar7_regs_wdt
->start
, resource_size(ar7_regs_wdt
));
338 static void ar7_wdt_shutdown(struct platform_device
*pdev
)
341 ar7_wdt_disable_wdt();
344 static struct platform_driver ar7_wdt_driver
= {
345 .probe
= ar7_wdt_probe
,
346 .remove
= __devexit_p(ar7_wdt_remove
),
347 .shutdown
= ar7_wdt_shutdown
,
349 .owner
= THIS_MODULE
,
354 module_platform_driver(ar7_wdt_driver
);