blob: 7fe4f7c3f7ce362112c0b1798233f8fae60e8a09 [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
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700154static void xwdt_clk_disable_unprepare(void *data)
155{
156 clk_disable_unprepare(data);
157}
158
Bill Pemberton2d991a12012-11-19 13:21:41 -0500159static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100160{
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700161 struct device *dev = &pdev->dev;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100162 int rc;
Michal Simek8d6a1402014-02-12 14:41:25 +0100163 u32 pfreq = 0, enable_once = 0;
Michal Simek90663172014-02-12 14:41:19 +0100164 struct xwdt_device *xdev;
Michal Simek90663172014-02-12 14:41:19 +0100165 struct watchdog_device *xilinx_wdt_wdd;
166
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700167 xdev = devm_kzalloc(dev, sizeof(*xdev), GFP_KERNEL);
Michal Simek90663172014-02-12 14:41:19 +0100168 if (!xdev)
169 return -ENOMEM;
170
171 xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
172 xilinx_wdt_wdd->info = &xilinx_wdt_ident;
173 xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700174 xilinx_wdt_wdd->parent = dev;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100175
Guenter Roeck0f0a6a22019-04-02 12:01:53 -0700176 xdev->base = devm_platform_ioremap_resource(pdev, 0);
Michal Simek90663172014-02-12 14:41:19 +0100177 if (IS_ERR(xdev->base))
178 return PTR_ERR(xdev->base);
Michal Simekf06cdfd2014-02-12 14:34:34 +0100179
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700180 rc = of_property_read_u32(dev->of_node, "xlnx,wdt-interval",
Michal Simek2e79a362014-02-12 14:41:21 +0100181 &xdev->wdt_interval);
Michal Simek8d6a1402014-02-12 14:41:25 +0100182 if (rc)
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700183 dev_warn(dev, "Parameter \"xlnx,wdt-interval\" not found\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100184
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700185 rc = of_property_read_u32(dev->of_node, "xlnx,wdt-enable-once",
Michal Simek2e79a362014-02-12 14:41:21 +0100186 &enable_once);
187 if (rc)
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700188 dev_warn(dev,
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100189 "Parameter \"xlnx,wdt-enable-once\" not found\n");
Michal Simek2e79a362014-02-12 14:41:21 +0100190
191 watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100192
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700193 xdev->clk = devm_clk_get(dev, NULL);
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200194 if (IS_ERR(xdev->clk)) {
195 if (PTR_ERR(xdev->clk) != -ENOENT)
196 return PTR_ERR(xdev->clk);
197
198 /*
199 * Clock framework support is optional, continue on
200 * anyways if we don't find a matching clock.
201 */
202 xdev->clk = NULL;
203
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700204 rc = of_property_read_u32(dev->of_node, "clock-frequency",
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200205 &pfreq);
206 if (rc)
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700207 dev_warn(dev,
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200208 "The watchdog clock freq cannot be obtained\n");
209 } else {
210 pfreq = clk_get_rate(xdev->clk);
211 }
212
Michal Simek75b3c5a2014-02-12 14:41:22 +0100213 /*
214 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
215 * ignored (interrupt), reset is only generated at second wdt overflow
216 */
Michal Simek8d6a1402014-02-12 14:41:25 +0100217 if (pfreq && xdev->wdt_interval)
Michal Simek90663172014-02-12 14:41:19 +0100218 xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
Michal Simek2e79a362014-02-12 14:41:21 +0100219 pfreq);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100220
Michal Simek90663172014-02-12 14:41:19 +0100221 spin_lock_init(&xdev->spinlock);
222 watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
223
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530224 rc = clk_prepare_enable(xdev->clk);
225 if (rc) {
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700226 dev_err(dev, "unable to enable clock\n");
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530227 return rc;
228 }
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700229 rc = devm_add_action_or_reset(dev, xwdt_clk_disable_unprepare,
230 xdev->clk);
231 if (rc)
232 return rc;
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530233
Michal Simek90663172014-02-12 14:41:19 +0100234 rc = xwdt_selftest(xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100235 if (rc == XWT_TIMER_FAILED) {
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700236 dev_err(dev, "SelfTest routine error\n");
237 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100238 }
239
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700240 rc = devm_watchdog_register_device(dev, xilinx_wdt_wdd);
Wolfram Sang0fa6cf72019-05-18 23:27:44 +0200241 if (rc)
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700242 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100243
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200244 clk_disable(xdev->clk);
245
Guenter Roeck801cdffe2019-04-09 10:23:48 -0700246 dev_info(dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
Michal Simek90663172014-02-12 14:41:19 +0100247 xdev->base, xilinx_wdt_wdd->timeout);
248
249 platform_set_drvdata(pdev, xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100250
251 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100252}
253
Michal Simek6f671c62017-08-07 13:24:23 +0200254/**
255 * xwdt_suspend - Suspend the device.
256 *
257 * @dev: handle to the device structure.
258 * Return: 0 always.
259 */
260static int __maybe_unused xwdt_suspend(struct device *dev)
261{
Wolfram Sang20745632018-04-19 16:06:29 +0200262 struct xwdt_device *xdev = dev_get_drvdata(dev);
Michal Simek6f671c62017-08-07 13:24:23 +0200263
264 if (watchdog_active(&xdev->xilinx_wdt_wdd))
265 xilinx_wdt_stop(&xdev->xilinx_wdt_wdd);
266
267 return 0;
268}
269
270/**
271 * xwdt_resume - Resume the device.
272 *
273 * @dev: handle to the device structure.
274 * Return: 0 on success, errno otherwise.
275 */
276static int __maybe_unused xwdt_resume(struct device *dev)
277{
Wolfram Sang20745632018-04-19 16:06:29 +0200278 struct xwdt_device *xdev = dev_get_drvdata(dev);
Michal Simek6f671c62017-08-07 13:24:23 +0200279 int ret = 0;
280
281 if (watchdog_active(&xdev->xilinx_wdt_wdd))
282 ret = xilinx_wdt_start(&xdev->xilinx_wdt_wdd);
283
284 return ret;
285}
286
287static SIMPLE_DEV_PM_OPS(xwdt_pm_ops, xwdt_suspend, xwdt_resume);
288
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100289/* Match table for of_platform binding */
Jingoo Han9ebf1852014-05-07 17:42:22 +0900290static const struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200291 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100292 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
293 {},
294};
295MODULE_DEVICE_TABLE(of, xwdt_of_match);
296
297static struct platform_driver xwdt_driver = {
298 .probe = xwdt_probe,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100299 .driver = {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100300 .name = WATCHDOG_NAME,
301 .of_match_table = xwdt_of_match,
Michal Simek6f671c62017-08-07 13:24:23 +0200302 .pm = &xwdt_pm_ops,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100303 },
304};
305
Axel Linb8ec6112011-11-29 13:56:27 +0800306module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100307
308MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
309MODULE_DESCRIPTION("Xilinx Watchdog driver");
Marcus Folkesson2e62c492018-03-16 16:14:11 +0100310MODULE_LICENSE("GPL");