2 * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
4 * (C) Copyright 2013 - 2014 Xilinx, Inc.
5 * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/ioport.h>
18 #include <linux/watchdog.h>
21 #include <linux/of_device.h>
22 #include <linux/of_address.h>
24 /* Register offsets for the Wdt device */
25 #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
26 #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
27 #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
29 /* Control/Status Register Masks */
30 #define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
31 #define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
32 #define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
34 /* Control/Status Register 0/1 bits */
35 #define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
37 /* SelfTest constants */
38 #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
39 #define XWT_TIMER_FAILED 0xFFFFFFFF
41 #define WATCHDOG_NAME "Xilinx Watchdog"
47 struct watchdog_device xilinx_wdt_wdd
;
50 static int xilinx_wdt_start(struct watchdog_device
*wdd
)
52 u32 control_status_reg
;
53 struct xwdt_device
*xdev
= watchdog_get_drvdata(wdd
);
55 spin_lock(&xdev
->spinlock
);
57 /* Clean previous status and enable the watchdog timer */
58 control_status_reg
= ioread32(xdev
->base
+ XWT_TWCSR0_OFFSET
);
59 control_status_reg
|= (XWT_CSR0_WRS_MASK
| XWT_CSR0_WDS_MASK
);
61 iowrite32((control_status_reg
| XWT_CSR0_EWDT1_MASK
),
62 xdev
->base
+ XWT_TWCSR0_OFFSET
);
64 iowrite32(XWT_CSRX_EWDT2_MASK
, xdev
->base
+ XWT_TWCSR1_OFFSET
);
66 spin_unlock(&xdev
->spinlock
);
71 static int xilinx_wdt_stop(struct watchdog_device
*wdd
)
73 u32 control_status_reg
;
74 struct xwdt_device
*xdev
= watchdog_get_drvdata(wdd
);
76 spin_lock(&xdev
->spinlock
);
78 control_status_reg
= ioread32(xdev
->base
+ XWT_TWCSR0_OFFSET
);
80 iowrite32((control_status_reg
& ~XWT_CSR0_EWDT1_MASK
),
81 xdev
->base
+ XWT_TWCSR0_OFFSET
);
83 iowrite32(0, xdev
->base
+ XWT_TWCSR1_OFFSET
);
85 spin_unlock(&xdev
->spinlock
);
86 pr_info("Stopped!\n");
91 static int xilinx_wdt_keepalive(struct watchdog_device
*wdd
)
93 u32 control_status_reg
;
94 struct xwdt_device
*xdev
= watchdog_get_drvdata(wdd
);
96 spin_lock(&xdev
->spinlock
);
98 control_status_reg
= ioread32(xdev
->base
+ XWT_TWCSR0_OFFSET
);
99 control_status_reg
|= (XWT_CSR0_WRS_MASK
| XWT_CSR0_WDS_MASK
);
100 iowrite32(control_status_reg
, xdev
->base
+ XWT_TWCSR0_OFFSET
);
102 spin_unlock(&xdev
->spinlock
);
107 static const struct watchdog_info xilinx_wdt_ident
= {
108 .options
= WDIOF_MAGICCLOSE
|
110 .firmware_version
= 1,
111 .identity
= WATCHDOG_NAME
,
114 static const struct watchdog_ops xilinx_wdt_ops
= {
115 .owner
= THIS_MODULE
,
116 .start
= xilinx_wdt_start
,
117 .stop
= xilinx_wdt_stop
,
118 .ping
= xilinx_wdt_keepalive
,
121 static u32
xwdt_selftest(struct xwdt_device
*xdev
)
127 spin_lock(&xdev
->spinlock
);
129 timer_value1
= ioread32(xdev
->base
+ XWT_TBR_OFFSET
);
130 timer_value2
= ioread32(xdev
->base
+ XWT_TBR_OFFSET
);
133 ((i
<= XWT_MAX_SELFTEST_LOOP_COUNT
) &&
134 (timer_value2
== timer_value1
)); i
++) {
135 timer_value2
= ioread32(xdev
->base
+ XWT_TBR_OFFSET
);
138 spin_unlock(&xdev
->spinlock
);
140 if (timer_value2
!= timer_value1
)
141 return ~XWT_TIMER_FAILED
;
143 return XWT_TIMER_FAILED
;
146 static int xwdt_probe(struct platform_device
*pdev
)
149 u32 pfreq
= 0, enable_once
= 0;
150 struct resource
*res
;
151 struct xwdt_device
*xdev
;
152 struct watchdog_device
*xilinx_wdt_wdd
;
154 xdev
= devm_kzalloc(&pdev
->dev
, sizeof(*xdev
), GFP_KERNEL
);
158 xilinx_wdt_wdd
= &xdev
->xilinx_wdt_wdd
;
159 xilinx_wdt_wdd
->info
= &xilinx_wdt_ident
;
160 xilinx_wdt_wdd
->ops
= &xilinx_wdt_ops
;
161 xilinx_wdt_wdd
->parent
= &pdev
->dev
;
163 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
164 xdev
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
165 if (IS_ERR(xdev
->base
))
166 return PTR_ERR(xdev
->base
);
168 rc
= of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency", &pfreq
);
171 "The watchdog clock frequency cannot be obtained\n");
173 rc
= of_property_read_u32(pdev
->dev
.of_node
, "xlnx,wdt-interval",
174 &xdev
->wdt_interval
);
177 "Parameter \"xlnx,wdt-interval\" not found\n");
179 rc
= of_property_read_u32(pdev
->dev
.of_node
, "xlnx,wdt-enable-once",
183 "Parameter \"xlnx,wdt-enable-once\" not found\n");
185 watchdog_set_nowayout(xilinx_wdt_wdd
, enable_once
);
188 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
189 * ignored (interrupt), reset is only generated at second wdt overflow
191 if (pfreq
&& xdev
->wdt_interval
)
192 xilinx_wdt_wdd
->timeout
= 2 * ((1 << xdev
->wdt_interval
) /
195 spin_lock_init(&xdev
->spinlock
);
196 watchdog_set_drvdata(xilinx_wdt_wdd
, xdev
);
198 rc
= xwdt_selftest(xdev
);
199 if (rc
== XWT_TIMER_FAILED
) {
200 dev_err(&pdev
->dev
, "SelfTest routine error\n");
204 rc
= watchdog_register_device(xilinx_wdt_wdd
);
206 dev_err(&pdev
->dev
, "Cannot register watchdog (err=%d)\n", rc
);
210 dev_info(&pdev
->dev
, "Xilinx Watchdog Timer at %p with timeout %ds\n",
211 xdev
->base
, xilinx_wdt_wdd
->timeout
);
213 platform_set_drvdata(pdev
, xdev
);
218 static int xwdt_remove(struct platform_device
*pdev
)
220 struct xwdt_device
*xdev
= platform_get_drvdata(pdev
);
222 watchdog_unregister_device(&xdev
->xilinx_wdt_wdd
);
227 /* Match table for of_platform binding */
228 static const struct of_device_id xwdt_of_match
[] = {
229 { .compatible
= "xlnx,xps-timebase-wdt-1.00.a", },
230 { .compatible
= "xlnx,xps-timebase-wdt-1.01.a", },
233 MODULE_DEVICE_TABLE(of
, xwdt_of_match
);
235 static struct platform_driver xwdt_driver
= {
237 .remove
= xwdt_remove
,
239 .name
= WATCHDOG_NAME
,
240 .of_match_table
= xwdt_of_match
,
244 module_platform_driver(xwdt_driver
);
246 MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
247 MODULE_DESCRIPTION("Xilinx Watchdog driver");
248 MODULE_LICENSE("GPL v2");