blob: d3f7eb0466782f4b386895cf509c54213a31819d [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Alejandro Cabrerae9659e62011-06-02 22:13:11 +01002/*
Michal Simek9419c072013-05-31 07:56:33 +02003 * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
4 *
Michal Simekd14fd962014-02-12 14:34:32 +01005 * (C) Copyright 2013 - 2014 Xilinx, Inc.
Michal Simek9419c072013-05-31 07:56:33 +02006 * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
Michal Simek9419c072013-05-31 07:56:33 +02007 */
Alejandro Cabrerae9659e62011-06-02 22:13:11 +01008
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +05309#include <linux/clk.h>
Michal Simekf06cdfd2014-02-12 14:34:34 +010010#include <linux/err.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010011#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/kernel.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010014#include <linux/ioport.h>
15#include <linux/watchdog.h>
16#include <linux/io.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010017#include <linux/of.h>
18#include <linux/of_device.h>
19#include <linux/of_address.h>
20
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 */
25
26/* Control/Status Register Masks */
27#define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
28#define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
29#define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
30
31/* Control/Status Register 0/1 bits */
32#define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
33
34/* SelfTest constants */
35#define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
36#define XWT_TIMER_FAILED 0xFFFFFFFF
37
38#define WATCHDOG_NAME "Xilinx Watchdog"
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010039
40struct xwdt_device {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010041 void __iomem *base;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010042 u32 wdt_interval;
Michal Simek90663172014-02-12 14:41:19 +010043 spinlock_t spinlock;
44 struct watchdog_device xilinx_wdt_wdd;
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +053045 struct clk *clk;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010046};
47
Michal Simekd14fd962014-02-12 14:34:32 +010048static int xilinx_wdt_start(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010049{
Maulik Jodhanib6bc4162017-08-07 13:24:22 +020050 int ret;
Michal Simek5cf4e692014-02-12 14:34:33 +010051 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010052 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010053
Maulik Jodhanib6bc4162017-08-07 13:24:22 +020054 ret = clk_enable(xdev->clk);
55 if (ret) {
56 dev_err(wdd->parent, "Failed to enable clock\n");
57 return ret;
58 }
59
Michal Simek90663172014-02-12 14:41:19 +010060 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010061
62 /* Clean previous status and enable the watchdog timer */
Michal Simek90663172014-02-12 14:41:19 +010063 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010064 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
65
66 iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010067 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010068
Michal Simek90663172014-02-12 14:41:19 +010069 iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010070
Michal Simek90663172014-02-12 14:41:19 +010071 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +010072
73 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010074}
75
Michal Simekd14fd962014-02-12 14:34:32 +010076static int xilinx_wdt_stop(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010077{
Michal Simek5cf4e692014-02-12 14:34:33 +010078 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010079 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010080
Michal Simek90663172014-02-12 14:41:19 +010081 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010082
Michal Simek90663172014-02-12 14:41:19 +010083 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010084
85 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010086 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010087
Michal Simek90663172014-02-12 14:41:19 +010088 iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010089
Michal Simek90663172014-02-12 14:41:19 +010090 spin_unlock(&xdev->spinlock);
Maulik Jodhanib6bc4162017-08-07 13:24:22 +020091
92 clk_disable(xdev->clk);
93
Joe Perches27c766a2012-02-15 15:06:19 -080094 pr_info("Stopped!\n");
Michal Simekd14fd962014-02-12 14:34:32 +010095
96 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010097}
98
Michal Simekd14fd962014-02-12 14:34:32 +010099static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100100{
Michal Simek5cf4e692014-02-12 14:34:33 +0100101 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +0100102 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +0100103
Michal Simek90663172014-02-12 14:41:19 +0100104 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100105
Michal Simek90663172014-02-12 14:41:19 +0100106 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100107 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
Michal Simek90663172014-02-12 14:41:19 +0100108 iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100109
Michal Simek90663172014-02-12 14:41:19 +0100110 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +0100111
112 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100113}
114
Michal Simekd14fd962014-02-12 14:34:32 +0100115static const struct watchdog_info xilinx_wdt_ident = {
116 .options = WDIOF_MAGICCLOSE |
117 WDIOF_KEEPALIVEPING,
118 .firmware_version = 1,
119 .identity = WATCHDOG_NAME,
120};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100121
Michal Simekd14fd962014-02-12 14:34:32 +0100122static const struct watchdog_ops xilinx_wdt_ops = {
123 .owner = THIS_MODULE,
124 .start = xilinx_wdt_start,
125 .stop = xilinx_wdt_stop,
126 .ping = xilinx_wdt_keepalive,
127};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100128
Michal Simek90663172014-02-12 14:41:19 +0100129static u32 xwdt_selftest(struct xwdt_device *xdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100130{
131 int i;
132 u32 timer_value1;
133 u32 timer_value2;
134
Michal Simek90663172014-02-12 14:41:19 +0100135 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100136
Michal Simek90663172014-02-12 14:41:19 +0100137 timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
138 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100139
140 for (i = 0;
141 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
142 (timer_value2 == timer_value1)); i++) {
Michal Simek90663172014-02-12 14:41:19 +0100143 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100144 }
145
Michal Simek90663172014-02-12 14:41:19 +0100146 spin_unlock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100147
148 if (timer_value2 != timer_value1)
149 return ~XWT_TIMER_FAILED;
150 else
151 return XWT_TIMER_FAILED;
152}
153
Bill Pemberton2d991a12012-11-19 13:21:41 -0500154static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100155{
156 int rc;
Michal Simek8d6a1402014-02-12 14:41:25 +0100157 u32 pfreq = 0, enable_once = 0;
Michal Simekf06cdfd2014-02-12 14:34:34 +0100158 struct resource *res;
Michal Simek90663172014-02-12 14:41:19 +0100159 struct xwdt_device *xdev;
Michal Simek90663172014-02-12 14:41:19 +0100160 struct watchdog_device *xilinx_wdt_wdd;
161
162 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
163 if (!xdev)
164 return -ENOMEM;
165
166 xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
167 xilinx_wdt_wdd->info = &xilinx_wdt_ident;
168 xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
169 xilinx_wdt_wdd->parent = &pdev->dev;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100170
Michal Simekf06cdfd2014-02-12 14:34:34 +0100171 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Michal Simek90663172014-02-12 14:41:19 +0100172 xdev->base = devm_ioremap_resource(&pdev->dev, res);
173 if (IS_ERR(xdev->base))
174 return PTR_ERR(xdev->base);
Michal Simekf06cdfd2014-02-12 14:34:34 +0100175
Michal Simek2e79a362014-02-12 14:41:21 +0100176 rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-interval",
177 &xdev->wdt_interval);
Michal Simek8d6a1402014-02-12 14:41:25 +0100178 if (rc)
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100179 dev_warn(&pdev->dev,
180 "Parameter \"xlnx,wdt-interval\" not found\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100181
Michal Simek2e79a362014-02-12 14:41:21 +0100182 rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-enable-once",
183 &enable_once);
184 if (rc)
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100185 dev_warn(&pdev->dev,
186 "Parameter \"xlnx,wdt-enable-once\" not found\n");
Michal Simek2e79a362014-02-12 14:41:21 +0100187
188 watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100189
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200190 xdev->clk = devm_clk_get(&pdev->dev, NULL);
191 if (IS_ERR(xdev->clk)) {
192 if (PTR_ERR(xdev->clk) != -ENOENT)
193 return PTR_ERR(xdev->clk);
194
195 /*
196 * Clock framework support is optional, continue on
197 * anyways if we don't find a matching clock.
198 */
199 xdev->clk = NULL;
200
201 rc = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
202 &pfreq);
203 if (rc)
204 dev_warn(&pdev->dev,
205 "The watchdog clock freq cannot be obtained\n");
206 } else {
207 pfreq = clk_get_rate(xdev->clk);
208 }
209
Michal Simek75b3c5a2014-02-12 14:41:22 +0100210 /*
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
213 */
Michal Simek8d6a1402014-02-12 14:41:25 +0100214 if (pfreq && xdev->wdt_interval)
Michal Simek90663172014-02-12 14:41:19 +0100215 xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
Michal Simek2e79a362014-02-12 14:41:21 +0100216 pfreq);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100217
Michal Simek90663172014-02-12 14:41:19 +0100218 spin_lock_init(&xdev->spinlock);
219 watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
220
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530221 rc = clk_prepare_enable(xdev->clk);
222 if (rc) {
223 dev_err(&pdev->dev, "unable to enable clock\n");
224 return rc;
225 }
226
Michal Simek90663172014-02-12 14:41:19 +0100227 rc = xwdt_selftest(xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100228 if (rc == XWT_TIMER_FAILED) {
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100229 dev_err(&pdev->dev, "SelfTest routine error\n");
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530230 goto err_clk_disable;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100231 }
232
Michal Simek90663172014-02-12 14:41:19 +0100233 rc = watchdog_register_device(xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100234 if (rc) {
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100235 dev_err(&pdev->dev, "Cannot register watchdog (err=%d)\n", rc);
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530236 goto err_clk_disable;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100237 }
238
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200239 clk_disable(xdev->clk);
240
Michal Simekd14fd962014-02-12 14:34:32 +0100241 dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
Michal Simek90663172014-02-12 14:41:19 +0100242 xdev->base, xilinx_wdt_wdd->timeout);
243
244 platform_set_drvdata(pdev, xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100245
246 return 0;
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530247err_clk_disable:
248 clk_disable_unprepare(xdev->clk);
249
250 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100251}
252
Michal Simek90663172014-02-12 14:41:19 +0100253static int xwdt_remove(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100254{
Michal Simek90663172014-02-12 14:41:19 +0100255 struct xwdt_device *xdev = platform_get_drvdata(pdev);
256
257 watchdog_unregister_device(&xdev->xilinx_wdt_wdd);
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530258 clk_disable_unprepare(xdev->clk);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100259
260 return 0;
261}
262
Michal Simek6f671c62017-08-07 13:24:23 +0200263/**
264 * xwdt_suspend - Suspend the device.
265 *
266 * @dev: handle to the device structure.
267 * Return: 0 always.
268 */
269static int __maybe_unused xwdt_suspend(struct device *dev)
270{
Wolfram Sang20745632018-04-19 16:06:29 +0200271 struct xwdt_device *xdev = dev_get_drvdata(dev);
Michal Simek6f671c62017-08-07 13:24:23 +0200272
273 if (watchdog_active(&xdev->xilinx_wdt_wdd))
274 xilinx_wdt_stop(&xdev->xilinx_wdt_wdd);
275
276 return 0;
277}
278
279/**
280 * xwdt_resume - Resume the device.
281 *
282 * @dev: handle to the device structure.
283 * Return: 0 on success, errno otherwise.
284 */
285static int __maybe_unused xwdt_resume(struct device *dev)
286{
Wolfram Sang20745632018-04-19 16:06:29 +0200287 struct xwdt_device *xdev = dev_get_drvdata(dev);
Michal Simek6f671c62017-08-07 13:24:23 +0200288 int ret = 0;
289
290 if (watchdog_active(&xdev->xilinx_wdt_wdd))
291 ret = xilinx_wdt_start(&xdev->xilinx_wdt_wdd);
292
293 return ret;
294}
295
296static SIMPLE_DEV_PM_OPS(xwdt_pm_ops, xwdt_suspend, xwdt_resume);
297
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100298/* Match table for of_platform binding */
Jingoo Han9ebf1852014-05-07 17:42:22 +0900299static const struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200300 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100301 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
302 {},
303};
304MODULE_DEVICE_TABLE(of, xwdt_of_match);
305
306static struct platform_driver xwdt_driver = {
307 .probe = xwdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500308 .remove = xwdt_remove,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100309 .driver = {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100310 .name = WATCHDOG_NAME,
311 .of_match_table = xwdt_of_match,
Michal Simek6f671c62017-08-07 13:24:23 +0200312 .pm = &xwdt_pm_ops,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100313 },
314};
315
Axel Linb8ec6112011-11-29 13:56:27 +0800316module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100317
318MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
319MODULE_DESCRIPTION("Xilinx Watchdog driver");
Marcus Folkesson2e62c492018-03-16 16:14:11 +0100320MODULE_LICENSE("GPL");