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/types.h>
14 #include <linux/kernel.h>
15 #include <linux/ioport.h>
16 #include <linux/watchdog.h>
19 #include <linux/of_device.h>
20 #include <linux/of_address.h>
22 /* Register offsets for the Wdt device */
23 #define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
24 #define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
25 #define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
27 /* Control/Status Register Masks */
28 #define XWT_CSR0_WRS_MASK BIT(3) /* Reset status */
29 #define XWT_CSR0_WDS_MASK BIT(2) /* Timer state */
30 #define XWT_CSR0_EWDT1_MASK BIT(1) /* Enable bit 1 */
32 /* Control/Status Register 0/1 bits */
33 #define XWT_CSRX_EWDT2_MASK BIT(0) /* Enable bit 2 */
35 /* SelfTest constants */
36 #define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
37 #define XWT_TIMER_FAILED 0xFFFFFFFF
39 #define WATCHDOG_NAME "Xilinx Watchdog"
44 spinlock_t spinlock; /* spinlock for register handling */
45 struct watchdog_device xilinx_wdt_wdd;
49 static int xilinx_wdt_start(struct watchdog_device *wdd)
52 u32 control_status_reg;
53 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
55 ret = clk_enable(xdev->clk);
57 dev_err(wdd->parent, "Failed to enable clock\n");
61 spin_lock(&xdev->spinlock);
63 /* Clean previous status and enable the watchdog timer */
64 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
65 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
67 iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
68 xdev->base + XWT_TWCSR0_OFFSET);
70 iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
72 spin_unlock(&xdev->spinlock);
74 dev_dbg(wdd->parent, "Watchdog Started!\n");
79 static int xilinx_wdt_stop(struct watchdog_device *wdd)
81 u32 control_status_reg;
82 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
84 spin_lock(&xdev->spinlock);
86 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
88 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
89 xdev->base + XWT_TWCSR0_OFFSET);
91 iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
93 spin_unlock(&xdev->spinlock);
95 clk_disable(xdev->clk);
97 dev_dbg(wdd->parent, "Watchdog Stopped!\n");
102 static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
104 u32 control_status_reg;
105 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
107 spin_lock(&xdev->spinlock);
109 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
110 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
111 iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
113 spin_unlock(&xdev->spinlock);
118 static const struct watchdog_info xilinx_wdt_ident = {
119 .options = WDIOF_MAGICCLOSE |
121 .firmware_version = 1,
122 .identity = WATCHDOG_NAME,
125 static const struct watchdog_ops xilinx_wdt_ops = {
126 .owner = THIS_MODULE,
127 .start = xilinx_wdt_start,
128 .stop = xilinx_wdt_stop,
129 .ping = xilinx_wdt_keepalive,
132 static u32 xwdt_selftest(struct xwdt_device *xdev)
138 spin_lock(&xdev->spinlock);
140 timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
141 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
144 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
145 (timer_value2 == timer_value1)); i++) {
146 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
149 spin_unlock(&xdev->spinlock);
151 if (timer_value2 != timer_value1)
152 return ~XWT_TIMER_FAILED;
154 return XWT_TIMER_FAILED;
157 static int xwdt_probe(struct platform_device *pdev)
159 struct device *dev = &pdev->dev;
161 u32 pfreq = 0, enable_once = 0;
162 struct xwdt_device *xdev;
163 struct watchdog_device *xilinx_wdt_wdd;
165 xdev = devm_kzalloc(dev, sizeof(*xdev), GFP_KERNEL);
169 xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
170 xilinx_wdt_wdd->info = &xilinx_wdt_ident;
171 xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
172 xilinx_wdt_wdd->parent = dev;
174 xdev->base = devm_platform_ioremap_resource(pdev, 0);
175 if (IS_ERR(xdev->base))
176 return PTR_ERR(xdev->base);
178 rc = of_property_read_u32(dev->of_node, "xlnx,wdt-interval",
179 &xdev->wdt_interval);
181 dev_warn(dev, "Parameter \"xlnx,wdt-interval\" not found\n");
183 rc = of_property_read_u32(dev->of_node, "xlnx,wdt-enable-once",
187 "Parameter \"xlnx,wdt-enable-once\" not found\n");
189 watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
191 xdev->clk = devm_clk_get_enabled(dev, NULL);
192 if (IS_ERR(xdev->clk)) {
193 if (PTR_ERR(xdev->clk) != -ENOENT)
194 return PTR_ERR(xdev->clk);
197 * Clock framework support is optional, continue on
198 * anyways if we don't find a matching clock.
202 rc = of_property_read_u32(dev->of_node, "clock-frequency",
206 "The watchdog clock freq cannot be obtained\n");
208 pfreq = clk_get_rate(xdev->clk);
212 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
213 * ignored (interrupt), reset is only generated at second wdt overflow
215 if (pfreq && xdev->wdt_interval)
216 xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
219 spin_lock_init(&xdev->spinlock);
220 watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
222 rc = xwdt_selftest(xdev);
223 if (rc == XWT_TIMER_FAILED) {
224 dev_err(dev, "SelfTest routine error\n");
228 rc = devm_watchdog_register_device(dev, xilinx_wdt_wdd);
232 clk_disable(xdev->clk);
234 dev_info(dev, "Xilinx Watchdog Timer with timeout %ds\n",
235 xilinx_wdt_wdd->timeout);
237 platform_set_drvdata(pdev, xdev);
243 * xwdt_suspend - Suspend the device.
245 * @dev: handle to the device structure.
248 static int __maybe_unused xwdt_suspend(struct device *dev)
250 struct xwdt_device *xdev = dev_get_drvdata(dev);
252 if (watchdog_active(&xdev->xilinx_wdt_wdd))
253 xilinx_wdt_stop(&xdev->xilinx_wdt_wdd);
259 * xwdt_resume - Resume the device.
261 * @dev: handle to the device structure.
262 * Return: 0 on success, errno otherwise.
264 static int __maybe_unused xwdt_resume(struct device *dev)
266 struct xwdt_device *xdev = dev_get_drvdata(dev);
269 if (watchdog_active(&xdev->xilinx_wdt_wdd))
270 ret = xilinx_wdt_start(&xdev->xilinx_wdt_wdd);
275 static SIMPLE_DEV_PM_OPS(xwdt_pm_ops, xwdt_suspend, xwdt_resume);
277 /* Match table for of_platform binding */
278 static const struct of_device_id xwdt_of_match[] = {
279 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
280 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
283 MODULE_DEVICE_TABLE(of, xwdt_of_match);
285 static struct platform_driver xwdt_driver = {
288 .name = WATCHDOG_NAME,
289 .of_match_table = xwdt_of_match,
294 module_platform_driver(xwdt_driver);
296 MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
297 MODULE_DESCRIPTION("Xilinx Watchdog driver");
298 MODULE_LICENSE("GPL");