1 // SPDX-License-Identifier: GPL-2.0+
3 * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
5 * (C) Copyright 2013 - 2014 Xilinx, Inc.
6 * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/ioport.h>
17 #include <linux/watchdog.h>
21 /* Register offsets for the Wdt device */
22 #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
23 #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
24 #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
26 /* Control/Status Register Masks */
27 #define XWT_CSR0_WRS_MASK BIT(3) /* Reset status */
28 #define XWT_CSR0_WDS_MASK BIT(2) /* Timer state */
29 #define XWT_CSR0_EWDT1_MASK BIT(1) /* Enable bit 1 */
31 /* Control/Status Register 0/1 bits */
32 #define XWT_CSRX_EWDT2_MASK BIT(0) /* Enable bit 2 */
34 /* SelfTest constants */
35 #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
36 #define XWT_TIMER_FAILED 0xFFFFFFFF
38 #define WATCHDOG_NAME "Xilinx Watchdog"
43 spinlock_t spinlock
; /* spinlock for register handling */
44 struct watchdog_device xilinx_wdt_wdd
;
48 static int xilinx_wdt_start(struct watchdog_device
*wdd
)
51 u32 control_status_reg
;
52 struct xwdt_device
*xdev
= watchdog_get_drvdata(wdd
);
54 ret
= clk_enable(xdev
->clk
);
56 dev_err(wdd
->parent
, "Failed to enable clock\n");
60 spin_lock(&xdev
->spinlock
);
62 /* Clean previous status and enable the watchdog timer */
63 control_status_reg
= ioread32(xdev
->base
+ XWT_TWCSR0_OFFSET
);
64 control_status_reg
|= (XWT_CSR0_WRS_MASK
| XWT_CSR0_WDS_MASK
);
66 iowrite32((control_status_reg
| XWT_CSR0_EWDT1_MASK
),
67 xdev
->base
+ XWT_TWCSR0_OFFSET
);
69 iowrite32(XWT_CSRX_EWDT2_MASK
, xdev
->base
+ XWT_TWCSR1_OFFSET
);
71 spin_unlock(&xdev
->spinlock
);
73 dev_dbg(wdd
->parent
, "Watchdog Started!\n");
78 static int xilinx_wdt_stop(struct watchdog_device
*wdd
)
80 u32 control_status_reg
;
81 struct xwdt_device
*xdev
= watchdog_get_drvdata(wdd
);
83 spin_lock(&xdev
->spinlock
);
85 control_status_reg
= ioread32(xdev
->base
+ XWT_TWCSR0_OFFSET
);
87 iowrite32((control_status_reg
& ~XWT_CSR0_EWDT1_MASK
),
88 xdev
->base
+ XWT_TWCSR0_OFFSET
);
90 iowrite32(0, xdev
->base
+ XWT_TWCSR1_OFFSET
);
92 spin_unlock(&xdev
->spinlock
);
94 clk_disable(xdev
->clk
);
96 dev_dbg(wdd
->parent
, "Watchdog Stopped!\n");
101 static int xilinx_wdt_keepalive(struct watchdog_device
*wdd
)
103 u32 control_status_reg
;
104 struct xwdt_device
*xdev
= watchdog_get_drvdata(wdd
);
106 spin_lock(&xdev
->spinlock
);
108 control_status_reg
= ioread32(xdev
->base
+ XWT_TWCSR0_OFFSET
);
109 control_status_reg
|= (XWT_CSR0_WRS_MASK
| XWT_CSR0_WDS_MASK
);
110 iowrite32(control_status_reg
, xdev
->base
+ XWT_TWCSR0_OFFSET
);
112 spin_unlock(&xdev
->spinlock
);
117 static const struct watchdog_info xilinx_wdt_ident
= {
118 .options
= WDIOF_MAGICCLOSE
|
120 .firmware_version
= 1,
121 .identity
= WATCHDOG_NAME
,
124 static const struct watchdog_ops xilinx_wdt_ops
= {
125 .owner
= THIS_MODULE
,
126 .start
= xilinx_wdt_start
,
127 .stop
= xilinx_wdt_stop
,
128 .ping
= xilinx_wdt_keepalive
,
131 static u32
xwdt_selftest(struct xwdt_device
*xdev
)
137 spin_lock(&xdev
->spinlock
);
139 timer_value1
= ioread32(xdev
->base
+ XWT_TBR_OFFSET
);
140 timer_value2
= ioread32(xdev
->base
+ XWT_TBR_OFFSET
);
143 ((i
<= XWT_MAX_SELFTEST_LOOP_COUNT
) &&
144 (timer_value2
== timer_value1
)); i
++) {
145 timer_value2
= ioread32(xdev
->base
+ XWT_TBR_OFFSET
);
148 spin_unlock(&xdev
->spinlock
);
150 if (timer_value2
!= timer_value1
)
151 return ~XWT_TIMER_FAILED
;
153 return XWT_TIMER_FAILED
;
156 static int xwdt_probe(struct platform_device
*pdev
)
158 struct device
*dev
= &pdev
->dev
;
160 u32 pfreq
= 0, enable_once
= 0;
161 struct xwdt_device
*xdev
;
162 struct watchdog_device
*xilinx_wdt_wdd
;
164 xdev
= devm_kzalloc(dev
, sizeof(*xdev
), GFP_KERNEL
);
168 xilinx_wdt_wdd
= &xdev
->xilinx_wdt_wdd
;
169 xilinx_wdt_wdd
->info
= &xilinx_wdt_ident
;
170 xilinx_wdt_wdd
->ops
= &xilinx_wdt_ops
;
171 xilinx_wdt_wdd
->parent
= dev
;
173 xdev
->base
= devm_platform_ioremap_resource(pdev
, 0);
174 if (IS_ERR(xdev
->base
))
175 return PTR_ERR(xdev
->base
);
177 rc
= of_property_read_u32(dev
->of_node
, "xlnx,wdt-interval",
178 &xdev
->wdt_interval
);
180 dev_warn(dev
, "Parameter \"xlnx,wdt-interval\" not found\n");
182 rc
= of_property_read_u32(dev
->of_node
, "xlnx,wdt-enable-once",
186 "Parameter \"xlnx,wdt-enable-once\" not found\n");
188 watchdog_set_nowayout(xilinx_wdt_wdd
, enable_once
);
190 xdev
->clk
= devm_clk_get_prepared(dev
, NULL
);
191 if (IS_ERR(xdev
->clk
)) {
192 if (PTR_ERR(xdev
->clk
) != -ENOENT
)
193 return PTR_ERR(xdev
->clk
);
196 * Clock framework support is optional, continue on
197 * anyways if we don't find a matching clock.
201 rc
= of_property_read_u32(dev
->of_node
, "clock-frequency",
205 "The watchdog clock freq cannot be obtained\n");
207 pfreq
= clk_get_rate(xdev
->clk
);
211 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
212 * ignored (interrupt), reset is only generated at second wdt overflow
214 if (pfreq
&& xdev
->wdt_interval
)
215 xilinx_wdt_wdd
->timeout
= 2 * ((1 << xdev
->wdt_interval
) /
218 spin_lock_init(&xdev
->spinlock
);
219 watchdog_set_drvdata(xilinx_wdt_wdd
, xdev
);
221 rc
= clk_enable(xdev
->clk
);
223 dev_err(dev
, "unable to enable clock\n");
227 rc
= xwdt_selftest(xdev
);
228 if (rc
== XWT_TIMER_FAILED
) {
229 dev_err(dev
, "SelfTest routine error\n");
230 clk_disable(xdev
->clk
);
234 clk_disable(xdev
->clk
);
236 rc
= devm_watchdog_register_device(dev
, xilinx_wdt_wdd
);
240 dev_info(dev
, "Xilinx Watchdog Timer with timeout %ds\n",
241 xilinx_wdt_wdd
->timeout
);
243 platform_set_drvdata(pdev
, xdev
);
249 * xwdt_suspend - Suspend the device.
251 * @dev: handle to the device structure.
254 static int __maybe_unused
xwdt_suspend(struct device
*dev
)
256 struct xwdt_device
*xdev
= dev_get_drvdata(dev
);
258 if (watchdog_active(&xdev
->xilinx_wdt_wdd
))
259 xilinx_wdt_stop(&xdev
->xilinx_wdt_wdd
);
265 * xwdt_resume - Resume the device.
267 * @dev: handle to the device structure.
268 * Return: 0 on success, errno otherwise.
270 static int __maybe_unused
xwdt_resume(struct device
*dev
)
272 struct xwdt_device
*xdev
= dev_get_drvdata(dev
);
275 if (watchdog_active(&xdev
->xilinx_wdt_wdd
))
276 ret
= xilinx_wdt_start(&xdev
->xilinx_wdt_wdd
);
281 static SIMPLE_DEV_PM_OPS(xwdt_pm_ops
, xwdt_suspend
, xwdt_resume
);
283 /* Match table for of_platform binding */
284 static const struct of_device_id xwdt_of_match
[] = {
285 { .compatible
= "xlnx,xps-timebase-wdt-1.00.a", },
286 { .compatible
= "xlnx,xps-timebase-wdt-1.01.a", },
289 MODULE_DEVICE_TABLE(of
, xwdt_of_match
);
291 static struct platform_driver xwdt_driver
= {
294 .name
= WATCHDOG_NAME
,
295 .of_match_table
= xwdt_of_match
,
300 module_platform_driver(xwdt_driver
);
302 MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
303 MODULE_DESCRIPTION("Xilinx Watchdog driver");
304 MODULE_LICENSE("GPL");