2 * drivers/char/watchdog/sp805-wdt.c
4 * Watchdog driver for ARM SP805 watchdog module
6 * Copyright (C) 2010 ST Microelectronics
7 * Viresh Kumar<viresh.kumar@st.com>
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2 or later. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
14 #include <linux/device.h>
15 #include <linux/resource.h>
16 #include <linux/amba/bus.h>
17 #include <linux/bitops.h>
18 #include <linux/clk.h>
20 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/kernel.h>
24 #include <linux/math64.h>
25 #include <linux/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/types.h>
31 #include <linux/uaccess.h>
32 #include <linux/watchdog.h>
34 /* default timeout in seconds */
35 #define DEFAULT_TIMEOUT 60
37 #define MODULE_NAME "sp805-wdt"
39 /* watchdog register offsets and masks */
41 #define LOAD_MIN 0x00000001
42 #define LOAD_MAX 0xFFFFFFFF
43 #define WDTVALUE 0x004
44 #define WDTCONTROL 0x008
45 /* control register masks */
46 #define INT_ENABLE (1 << 0)
47 #define RESET_ENABLE (1 << 1)
48 #define WDTINTCLR 0x00C
51 #define INT_MASK (1 << 0)
53 #define UNLOCK 0x1ACCE551
54 #define LOCK 0x00000001
57 * struct sp805_wdt: sp805 wdt device structure
59 * lock: spin lock protecting dev structure and io access
60 * base: base address of wdt
61 * clk: clock structure of wdt
62 * dev: amba device structure of wdt
63 * status: current status of wdt
64 * load_val: load value to be set for current timeout
65 * timeout: current programmed timeout
71 struct amba_device
*adev
;
74 #define WDT_CAN_BE_CLOSED 1
75 unsigned int load_val
;
80 static struct sp805_wdt
*wdt
;
81 static int nowayout
= WATCHDOG_NOWAYOUT
;
83 /* This routine finds load value that will reset system in required timout */
84 static void wdt_setload(unsigned int timeout
)
88 rate
= clk_get_rate(wdt
->clk
);
91 * sp805 runs counter with given value twice, after the end of first
92 * counter it gives an interrupt and then starts counter again. If
93 * interrupt already occurred then it resets the system. This is why
94 * load is half of what should be required.
96 load
= div_u64(rate
, 2) * timeout
- 1;
98 load
= (load
> LOAD_MAX
) ? LOAD_MAX
: load
;
99 load
= (load
< LOAD_MIN
) ? LOAD_MIN
: load
;
101 spin_lock(&wdt
->lock
);
102 wdt
->load_val
= load
;
103 /* roundup timeout to closest positive integer value */
104 wdt
->timeout
= div_u64((load
+ 1) * 2 + (rate
/ 2), rate
);
105 spin_unlock(&wdt
->lock
);
108 /* returns number of seconds left for reset to occur */
109 static u32
wdt_timeleft(void)
113 rate
= clk_get_rate(wdt
->clk
);
115 spin_lock(&wdt
->lock
);
116 load
= readl(wdt
->base
+ WDTVALUE
);
118 /*If the interrupt is inactive then time left is WDTValue + WDTLoad. */
119 if (!(readl(wdt
->base
+ WDTRIS
) & INT_MASK
))
120 load
+= wdt
->load_val
+ 1;
121 spin_unlock(&wdt
->lock
);
123 return div_u64(load
, rate
);
126 /* enables watchdog timers reset */
127 static void wdt_enable(void)
129 spin_lock(&wdt
->lock
);
131 writel(UNLOCK
, wdt
->base
+ WDTLOCK
);
132 writel(wdt
->load_val
, wdt
->base
+ WDTLOAD
);
133 writel(INT_MASK
, wdt
->base
+ WDTINTCLR
);
134 writel(INT_ENABLE
| RESET_ENABLE
, wdt
->base
+ WDTCONTROL
);
135 writel(LOCK
, wdt
->base
+ WDTLOCK
);
137 /* Flush posted writes. */
138 readl(wdt
->base
+ WDTLOCK
);
139 spin_unlock(&wdt
->lock
);
142 /* disables watchdog timers reset */
143 static void wdt_disable(void)
145 spin_lock(&wdt
->lock
);
147 writel(UNLOCK
, wdt
->base
+ WDTLOCK
);
148 writel(0, wdt
->base
+ WDTCONTROL
);
149 writel(LOCK
, wdt
->base
+ WDTLOCK
);
151 /* Flush posted writes. */
152 readl(wdt
->base
+ WDTLOCK
);
153 spin_unlock(&wdt
->lock
);
156 static ssize_t
sp805_wdt_write(struct file
*file
, const char *data
,
157 size_t len
, loff_t
*ppos
)
163 clear_bit(WDT_CAN_BE_CLOSED
, &wdt
->status
);
165 for (i
= 0; i
!= len
; i
++) {
168 if (get_user(c
, data
+ i
))
170 /* Check for Magic Close character */
172 set_bit(WDT_CAN_BE_CLOSED
,
183 static const struct watchdog_info ident
= {
184 .options
= WDIOF_MAGICCLOSE
| WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
,
185 .identity
= MODULE_NAME
,
188 static long sp805_wdt_ioctl(struct file
*file
, unsigned int cmd
,
192 unsigned int timeout
;
195 case WDIOC_GETSUPPORT
:
196 ret
= copy_to_user((struct watchdog_info
*)arg
, &ident
,
197 sizeof(ident
)) ? -EFAULT
: 0;
200 case WDIOC_GETSTATUS
:
201 ret
= put_user(0, (int *)arg
);
204 case WDIOC_KEEPALIVE
:
209 case WDIOC_SETTIMEOUT
:
210 ret
= get_user(timeout
, (unsigned int *)arg
);
214 wdt_setload(timeout
);
219 case WDIOC_GETTIMEOUT
:
220 ret
= put_user(wdt
->timeout
, (unsigned int *)arg
);
222 case WDIOC_GETTIMELEFT
:
223 ret
= put_user(wdt_timeleft(), (unsigned int *)arg
);
229 static int sp805_wdt_open(struct inode
*inode
, struct file
*file
)
233 if (test_and_set_bit(WDT_BUSY
, &wdt
->status
))
236 ret
= clk_enable(wdt
->clk
);
238 dev_err(&wdt
->adev
->dev
, "clock enable fail");
244 /* can not be closed, once enabled */
245 clear_bit(WDT_CAN_BE_CLOSED
, &wdt
->status
);
246 return nonseekable_open(inode
, file
);
249 clear_bit(WDT_BUSY
, &wdt
->status
);
253 static int sp805_wdt_release(struct inode
*inode
, struct file
*file
)
255 if (!test_bit(WDT_CAN_BE_CLOSED
, &wdt
->status
)) {
256 clear_bit(WDT_BUSY
, &wdt
->status
);
257 dev_warn(&wdt
->adev
->dev
, "Device closed unexpectedly\n");
262 clk_disable(wdt
->clk
);
263 clear_bit(WDT_BUSY
, &wdt
->status
);
268 static const struct file_operations sp805_wdt_fops
= {
269 .owner
= THIS_MODULE
,
271 .write
= sp805_wdt_write
,
272 .unlocked_ioctl
= sp805_wdt_ioctl
,
273 .open
= sp805_wdt_open
,
274 .release
= sp805_wdt_release
,
277 static struct miscdevice sp805_wdt_miscdev
= {
278 .minor
= WATCHDOG_MINOR
,
280 .fops
= &sp805_wdt_fops
,
284 sp805_wdt_probe(struct amba_device
*adev
, const struct amba_id
*id
)
288 if (!request_mem_region(adev
->res
.start
, resource_size(&adev
->res
),
290 dev_warn(&adev
->dev
, "Failed to get memory region resource\n");
295 wdt
= kzalloc(sizeof(*wdt
), GFP_KERNEL
);
297 dev_warn(&adev
->dev
, "Kzalloc failed\n");
302 wdt
->clk
= clk_get(&adev
->dev
, NULL
);
303 if (IS_ERR(wdt
->clk
)) {
304 dev_warn(&adev
->dev
, "Clock not found\n");
305 ret
= PTR_ERR(wdt
->clk
);
309 wdt
->base
= ioremap(adev
->res
.start
, resource_size(&adev
->res
));
312 dev_warn(&adev
->dev
, "ioremap fail\n");
317 spin_lock_init(&wdt
->lock
);
318 wdt_setload(DEFAULT_TIMEOUT
);
320 ret
= misc_register(&sp805_wdt_miscdev
);
322 dev_warn(&adev
->dev
, "cannot register misc device\n");
323 goto err_misc_register
;
326 dev_info(&adev
->dev
, "registration successful\n");
337 release_mem_region(adev
->res
.start
, resource_size(&adev
->res
));
339 dev_err(&adev
->dev
, "Probe Failed!!!\n");
343 static int __devexit
sp805_wdt_remove(struct amba_device
*adev
)
345 misc_deregister(&sp805_wdt_miscdev
);
349 release_mem_region(adev
->res
.start
, resource_size(&adev
->res
));
354 static struct amba_id sp805_wdt_ids
[] __initdata
= {
362 static struct amba_driver sp805_wdt_driver
= {
366 .id_table
= sp805_wdt_ids
,
367 .probe
= sp805_wdt_probe
,
368 .remove
= __devexit_p(sp805_wdt_remove
),
371 static int __init
sp805_wdt_init(void)
373 return amba_driver_register(&sp805_wdt_driver
);
375 module_init(sp805_wdt_init
);
377 static void __exit
sp805_wdt_exit(void)
379 amba_driver_unregister(&sp805_wdt_driver
);
381 module_exit(sp805_wdt_exit
);
383 module_param(nowayout
, int, 0);
384 MODULE_PARM_DESC(nowayout
,
385 "Set to 1 to keep watchdog running after device release");
387 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
388 MODULE_DESCRIPTION("ARM SP805 Watchdog Driver");
389 MODULE_LICENSE("GPL");
390 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);