2 * of_xilinx_wdt.c 1.01 A Watchdog Device Driver for Xilinx xps_timebase_wdt
4 * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
6 * -----------------------
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * -----------------------
14 * 30-May-2011 Alejandro Cabrera <aldaya@gmail.com>
15 * - If "xlnx,wdt-enable-once" wasn't found on device tree the
16 * module will use CONFIG_WATCHDOG_NOWAYOUT
17 * - If the device tree parameters ("clock-frequency" and
18 * "xlnx,wdt-interval") wasn't found the driver won't
19 * know the wdt reset interval
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
28 #include <linux/miscdevice.h>
29 #include <linux/init.h>
30 #include <linux/ioport.h>
31 #include <linux/watchdog.h>
33 #include <linux/uaccess.h>
35 #include <linux/of_device.h>
36 #include <linux/of_address.h>
38 /* Register offsets for the Wdt device */
39 #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
40 #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
41 #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
43 /* Control/Status Register Masks */
44 #define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
45 #define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
46 #define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
48 /* Control/Status Register 0/1 bits */
49 #define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
51 /* SelfTest constants */
52 #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
53 #define XWT_TIMER_FAILED 0xFFFFFFFF
55 #define WATCHDOG_NAME "Xilinx Watchdog"
56 #define PFX WATCHDOG_NAME ": "
66 static struct xwdt_device xdev
;
69 static u32 control_status_reg
;
70 static u8 expect_close
;
72 static unsigned long driver_open
;
74 static DEFINE_SPINLOCK(spinlock
);
76 static void xwdt_start(void)
80 /* Clean previous status and enable the watchdog timer */
81 control_status_reg
= ioread32(xdev
.base
+ XWT_TWCSR0_OFFSET
);
82 control_status_reg
|= (XWT_CSR0_WRS_MASK
| XWT_CSR0_WDS_MASK
);
84 iowrite32((control_status_reg
| XWT_CSR0_EWDT1_MASK
),
85 xdev
.base
+ XWT_TWCSR0_OFFSET
);
87 iowrite32(XWT_CSRX_EWDT2_MASK
, xdev
.base
+ XWT_TWCSR1_OFFSET
);
89 spin_unlock(&spinlock
);
92 static void xwdt_stop(void)
96 control_status_reg
= ioread32(xdev
.base
+ XWT_TWCSR0_OFFSET
);
98 iowrite32((control_status_reg
& ~XWT_CSR0_EWDT1_MASK
),
99 xdev
.base
+ XWT_TWCSR0_OFFSET
);
101 iowrite32(0, xdev
.base
+ XWT_TWCSR1_OFFSET
);
103 spin_unlock(&spinlock
);
104 pr_info("Stopped!\n");
107 static void xwdt_keepalive(void)
109 spin_lock(&spinlock
);
111 control_status_reg
= ioread32(xdev
.base
+ XWT_TWCSR0_OFFSET
);
112 control_status_reg
|= (XWT_CSR0_WRS_MASK
| XWT_CSR0_WDS_MASK
);
113 iowrite32(control_status_reg
, xdev
.base
+ XWT_TWCSR0_OFFSET
);
115 spin_unlock(&spinlock
);
118 static void xwdt_get_status(int *status
)
122 spin_lock(&spinlock
);
124 control_status_reg
= ioread32(xdev
.base
+ XWT_TWCSR0_OFFSET
);
125 new_status
= ((control_status_reg
&
126 (XWT_CSR0_WRS_MASK
| XWT_CSR0_WDS_MASK
)) != 0);
127 spin_unlock(&spinlock
);
131 *status
|= WDIOF_CARDRESET
;
134 static u32
xwdt_selftest(void)
140 spin_lock(&spinlock
);
142 timer_value1
= ioread32(xdev
.base
+ XWT_TBR_OFFSET
);
143 timer_value2
= ioread32(xdev
.base
+ XWT_TBR_OFFSET
);
146 ((i
<= XWT_MAX_SELFTEST_LOOP_COUNT
) &&
147 (timer_value2
== timer_value1
)); i
++) {
148 timer_value2
= ioread32(xdev
.base
+ XWT_TBR_OFFSET
);
151 spin_unlock(&spinlock
);
153 if (timer_value2
!= timer_value1
)
154 return ~XWT_TIMER_FAILED
;
156 return XWT_TIMER_FAILED
;
159 static int xwdt_open(struct inode
*inode
, struct file
*file
)
161 /* Only one process can handle the wdt at a time */
162 if (test_and_set_bit(0, &driver_open
))
165 /* Make sure that the module are always loaded...*/
167 __module_get(THIS_MODULE
);
170 pr_info("Started...\n");
172 return nonseekable_open(inode
, file
);
175 static int xwdt_release(struct inode
*inode
, struct file
*file
)
177 if (expect_close
== 42) {
180 pr_crit("Unexpected close, not stopping watchdog!\n");
184 clear_bit(0, &driver_open
);
191 * @file: file handle to the watchdog
192 * @buf: buffer to write (unused as data does not matter here
193 * @count: count of bytes
194 * @ppos: pointer to the position to write. No seeks allowed
196 * A write to a watchdog device is defined as a keepalive signal. Any
197 * write of data will do, as we don't define content meaning.
199 static ssize_t
xwdt_write(struct file
*file
, const char __user
*buf
,
200 size_t len
, loff_t
*ppos
)
203 if (!xdev
.nowayout
) {
206 /* In case it was set long ago */
209 for (i
= 0; i
!= len
; i
++) {
212 if (get_user(c
, buf
+ i
))
223 static const struct watchdog_info ident
= {
224 .options
= WDIOF_MAGICCLOSE
|
226 .firmware_version
= 1,
227 .identity
= WATCHDOG_NAME
,
232 * @file: file handle to the device
233 * @cmd: watchdog command
234 * @arg: argument pointer
236 * The watchdog API defines a common set of functions for all watchdogs
237 * according to their available features.
239 static long xwdt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
244 struct watchdog_info __user
*ident
;
248 uarg
.i
= (int __user
*)arg
;
251 case WDIOC_GETSUPPORT
:
252 return copy_to_user(uarg
.ident
, &ident
,
253 sizeof(ident
)) ? -EFAULT
: 0;
255 case WDIOC_GETBOOTSTATUS
:
256 return put_user(xdev
.boot_status
, uarg
.i
);
258 case WDIOC_GETSTATUS
:
259 xwdt_get_status(&status
);
260 return put_user(status
, uarg
.i
);
262 case WDIOC_KEEPALIVE
:
266 case WDIOC_GETTIMEOUT
:
270 return put_user(timeout
, uarg
.i
);
277 static const struct file_operations xwdt_fops
= {
278 .owner
= THIS_MODULE
,
282 .release
= xwdt_release
,
283 .unlocked_ioctl
= xwdt_ioctl
,
286 static struct miscdevice xwdt_miscdev
= {
287 .minor
= WATCHDOG_MINOR
,
292 static int xwdt_probe(struct platform_device
*pdev
)
300 pfreq
= (u32
*)of_get_property(pdev
->dev
.of_node
,
301 "clock-frequency", NULL
);
304 pr_warn("The watchdog clock frequency cannot be obtained!\n");
308 rc
= of_address_to_resource(pdev
->dev
.of_node
, 0, &xdev
.res
);
310 pr_warn("invalid address!\n");
314 tmptr
= (u32
*)of_get_property(pdev
->dev
.of_node
,
315 "xlnx,wdt-interval", NULL
);
317 pr_warn("Parameter \"xlnx,wdt-interval\" not found in device tree!\n");
320 xdev
.wdt_interval
= *tmptr
;
323 tmptr
= (u32
*)of_get_property(pdev
->dev
.of_node
,
324 "xlnx,wdt-enable-once", NULL
);
326 pr_warn("Parameter \"xlnx,wdt-enable-once\" not found in device tree!\n");
327 xdev
.nowayout
= WATCHDOG_NOWAYOUT
;
331 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
332 * ignored (interrupt), reset is only generated at second wdt overflow
335 timeout
= 2 * ((1<<xdev
.wdt_interval
) / *pfreq
);
337 if (!request_mem_region(xdev
.res
.start
,
338 xdev
.res
.end
- xdev
.res
.start
+ 1, WATCHDOG_NAME
)) {
340 pr_err("memory request failure!\n");
344 xdev
.base
= ioremap(xdev
.res
.start
, xdev
.res
.end
- xdev
.res
.start
+ 1);
345 if (xdev
.base
== NULL
) {
347 pr_err("ioremap failure!\n");
351 rc
= xwdt_selftest();
352 if (rc
== XWT_TIMER_FAILED
) {
353 pr_err("SelfTest routine error!\n");
357 xwdt_get_status(&xdev
.boot_status
);
359 rc
= misc_register(&xwdt_miscdev
);
361 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
362 xwdt_miscdev
.minor
, rc
);
367 pr_info("driver loaded (timeout=? sec, nowayout=%d)\n",
370 pr_info("driver loaded (timeout=%d sec, nowayout=%d)\n",
371 timeout
, xdev
.nowayout
);
374 clear_bit(0, &driver_open
);
381 release_mem_region(xdev
.res
.start
, resource_size(&xdev
.res
));
386 static int xwdt_remove(struct platform_device
*dev
)
388 misc_deregister(&xwdt_miscdev
);
390 release_mem_region(xdev
.res
.start
, resource_size(&xdev
.res
));
395 /* Match table for of_platform binding */
396 static struct of_device_id xwdt_of_match
[] = {
397 { .compatible
= "xlnx,xps-timebase-wdt-1.01.a", },
400 MODULE_DEVICE_TABLE(of
, xwdt_of_match
);
402 static struct platform_driver xwdt_driver
= {
404 .remove
= xwdt_remove
,
406 .owner
= THIS_MODULE
,
407 .name
= WATCHDOG_NAME
,
408 .of_match_table
= xwdt_of_match
,
412 module_platform_driver(xwdt_driver
);
414 MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
415 MODULE_DESCRIPTION("Xilinx Watchdog driver");
416 MODULE_LICENSE("GPL");
417 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);