2 * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
4 * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/types.h>
13 #include <linux/miscdevice.h>
14 #include <linux/watchdog.h>
16 #include <linux/reboot.h>
17 #include <linux/init.h>
18 #include <linux/uaccess.h>
19 #include <linux/platform_device.h>
20 #include <linux/clk.h>
21 #include <linux/err.h>
23 #include <asm/txx9tmr.h>
25 #define TIMER_MARGIN 60 /* Default is 60 seconds */
27 static int timeout
= TIMER_MARGIN
; /* in seconds */
28 module_param(timeout
, int, 0);
29 MODULE_PARM_DESC(timeout
,
30 "Watchdog timeout in seconds. "
31 "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS
) ")/(IMCLK/256)), "
32 "default=" __MODULE_STRING(TIMER_MARGIN
) ")");
34 static int nowayout
= WATCHDOG_NOWAYOUT
;
35 module_param(nowayout
, int, 0);
36 MODULE_PARM_DESC(nowayout
,
37 "Watchdog cannot be stopped once started "
38 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
40 #define WD_TIMER_CCD 7 /* 1/256 */
41 #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
42 #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
44 static unsigned long txx9wdt_alive
;
45 static int expect_close
;
46 static struct txx9_tmr_reg __iomem
*txx9wdt_reg
;
47 static struct clk
*txx9_imclk
;
48 static DEFINE_SPINLOCK(txx9_lock
);
50 static void txx9wdt_ping(void)
52 spin_lock(&txx9_lock
);
53 __raw_writel(TXx9_TMWTMR_TWIE
| TXx9_TMWTMR_TWC
, &txx9wdt_reg
->wtmr
);
54 spin_unlock(&txx9_lock
);
57 static void txx9wdt_start(void)
59 spin_lock(&txx9_lock
);
60 __raw_writel(WD_TIMER_CLK
* timeout
, &txx9wdt_reg
->cpra
);
61 __raw_writel(WD_TIMER_CCD
, &txx9wdt_reg
->ccdr
);
62 __raw_writel(0, &txx9wdt_reg
->tisr
); /* clear pending interrupt */
63 __raw_writel(TXx9_TMTCR_TCE
| TXx9_TMTCR_CCDE
| TXx9_TMTCR_TMODE_WDOG
,
65 __raw_writel(TXx9_TMWTMR_TWIE
| TXx9_TMWTMR_TWC
, &txx9wdt_reg
->wtmr
);
66 spin_unlock(&txx9_lock
);
69 static void txx9wdt_stop(void)
71 spin_lock(&txx9_lock
);
72 __raw_writel(TXx9_TMWTMR_WDIS
, &txx9wdt_reg
->wtmr
);
73 __raw_writel(__raw_readl(&txx9wdt_reg
->tcr
) & ~TXx9_TMTCR_TCE
,
75 spin_unlock(&txx9_lock
);
78 static int txx9wdt_open(struct inode
*inode
, struct file
*file
)
80 if (test_and_set_bit(0, &txx9wdt_alive
))
83 if (__raw_readl(&txx9wdt_reg
->tcr
) & TXx9_TMTCR_TCE
) {
84 clear_bit(0, &txx9wdt_alive
);
89 __module_get(THIS_MODULE
);
92 return nonseekable_open(inode
, file
);
95 static int txx9wdt_release(struct inode
*inode
, struct file
*file
)
100 printk(KERN_CRIT
"txx9wdt: "
101 "Unexpected close, not stopping watchdog!\n");
104 clear_bit(0, &txx9wdt_alive
);
109 static ssize_t
txx9wdt_write(struct file
*file
, const char __user
*data
,
110 size_t len
, loff_t
*ppos
)
117 for (i
= 0; i
!= len
; i
++) {
119 if (get_user(c
, data
+ i
))
130 static long txx9wdt_ioctl(struct file
*file
, unsigned int cmd
,
133 void __user
*argp
= (void __user
*)arg
;
134 int __user
*p
= argp
;
136 static const struct watchdog_info ident
= {
137 .options
= WDIOF_SETTIMEOUT
|
138 WDIOF_KEEPALIVEPING
|
140 .firmware_version
= 0,
141 .identity
= "Hardware Watchdog for TXx9",
145 case WDIOC_GETSUPPORT
:
146 return copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
147 case WDIOC_GETSTATUS
:
148 case WDIOC_GETBOOTSTATUS
:
149 return put_user(0, p
);
150 case WDIOC_KEEPALIVE
:
153 case WDIOC_SETTIMEOUT
:
154 if (get_user(new_timeout
, p
))
156 if (new_timeout
< 1 || new_timeout
> WD_MAX_TIMEOUT
)
158 timeout
= new_timeout
;
162 case WDIOC_GETTIMEOUT
:
163 return put_user(timeout
, p
);
169 static int txx9wdt_notify_sys(struct notifier_block
*this, unsigned long code
,
172 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
177 static const struct file_operations txx9wdt_fops
= {
178 .owner
= THIS_MODULE
,
180 .write
= txx9wdt_write
,
181 .unlocked_ioctl
= txx9wdt_ioctl
,
182 .open
= txx9wdt_open
,
183 .release
= txx9wdt_release
,
186 static struct miscdevice txx9wdt_miscdev
= {
187 .minor
= WATCHDOG_MINOR
,
189 .fops
= &txx9wdt_fops
,
192 static struct notifier_block txx9wdt_notifier
= {
193 .notifier_call
= txx9wdt_notify_sys
,
196 static int __init
txx9wdt_probe(struct platform_device
*dev
)
198 struct resource
*res
;
201 txx9_imclk
= clk_get(NULL
, "imbus_clk");
202 if (IS_ERR(txx9_imclk
)) {
203 ret
= PTR_ERR(txx9_imclk
);
207 ret
= clk_enable(txx9_imclk
);
214 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
217 if (!devm_request_mem_region(&dev
->dev
,
218 res
->start
, res
->end
- res
->start
+ 1,
221 txx9wdt_reg
= devm_ioremap(&dev
->dev
,
222 res
->start
, res
->end
- res
->start
+ 1);
226 ret
= register_reboot_notifier(&txx9wdt_notifier
);
230 ret
= misc_register(&txx9wdt_miscdev
);
232 unregister_reboot_notifier(&txx9wdt_notifier
);
236 printk(KERN_INFO
"Hardware Watchdog Timer for TXx9: "
237 "timeout=%d sec (max %ld) (nowayout= %d)\n",
238 timeout
, WD_MAX_TIMEOUT
, nowayout
);
245 clk_disable(txx9_imclk
);
251 static int __exit
txx9wdt_remove(struct platform_device
*dev
)
253 misc_deregister(&txx9wdt_miscdev
);
254 unregister_reboot_notifier(&txx9wdt_notifier
);
255 clk_disable(txx9_imclk
);
260 static struct platform_driver txx9wdt_driver
= {
261 .remove
= __exit_p(txx9wdt_remove
),
264 .owner
= THIS_MODULE
,
268 static int __init
watchdog_init(void)
270 return platform_driver_probe(&txx9wdt_driver
, txx9wdt_probe
);
273 static void __exit
watchdog_exit(void)
275 platform_driver_unregister(&txx9wdt_driver
);
278 module_init(watchdog_init
);
279 module_exit(watchdog_exit
);
281 MODULE_DESCRIPTION("TXx9 Watchdog Driver");
282 MODULE_LICENSE("GPL");
283 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
284 MODULE_ALIAS("platform:txx9wdt");