blob: 1cf286945b7a121dfcee7682e6fa7b0d55b84984 [file] [log] [blame]
Alejandro Cabrerae9659e62011-06-02 22:13:11 +01001/*
Michal Simek9419c072013-05-31 07:56:33 +02002 * Watchdog Device Driver for Xilinx axi/xps_timebase_wdt
3 *
Michal Simekd14fd962014-02-12 14:34:32 +01004 * (C) Copyright 2013 - 2014 Xilinx, Inc.
Michal Simek9419c072013-05-31 07:56:33 +02005 * (C) Copyright 2011 (Alejandro Cabrera <aldaya@gmail.com>)
6 *
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.
11 */
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010012
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +053013#include <linux/clk.h>
Michal Simekf06cdfd2014-02-12 14:34:34 +010014#include <linux/err.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010018#include <linux/ioport.h>
19#include <linux/watchdog.h>
20#include <linux/io.h>
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010021#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/of_address.h>
24
25/* Register offsets for the Wdt device */
26#define XWT_TWCSR0_OFFSET 0x0 /* Control/Status Register0 */
27#define XWT_TWCSR1_OFFSET 0x4 /* Control/Status Register1 */
28#define XWT_TBR_OFFSET 0x8 /* Timebase Register Offset */
29
30/* Control/Status Register Masks */
31#define XWT_CSR0_WRS_MASK 0x00000008 /* Reset status */
32#define XWT_CSR0_WDS_MASK 0x00000004 /* Timer state */
33#define XWT_CSR0_EWDT1_MASK 0x00000002 /* Enable bit 1 */
34
35/* Control/Status Register 0/1 bits */
36#define XWT_CSRX_EWDT2_MASK 0x00000001 /* Enable bit 2 */
37
38/* SelfTest constants */
39#define XWT_MAX_SELFTEST_LOOP_COUNT 0x00010000
40#define XWT_TIMER_FAILED 0xFFFFFFFF
41
42#define WATCHDOG_NAME "Xilinx Watchdog"
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010043
44struct xwdt_device {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010045 void __iomem *base;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010046 u32 wdt_interval;
Michal Simek90663172014-02-12 14:41:19 +010047 spinlock_t spinlock;
48 struct watchdog_device xilinx_wdt_wdd;
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +053049 struct clk *clk;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010050};
51
Michal Simekd14fd962014-02-12 14:34:32 +010052static int xilinx_wdt_start(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010053{
Maulik Jodhanib6bc4162017-08-07 13:24:22 +020054 int ret;
Michal Simek5cf4e692014-02-12 14:34:33 +010055 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010056 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010057
Maulik Jodhanib6bc4162017-08-07 13:24:22 +020058 ret = clk_enable(xdev->clk);
59 if (ret) {
60 dev_err(wdd->parent, "Failed to enable clock\n");
61 return ret;
62 }
63
Michal Simek90663172014-02-12 14:41:19 +010064 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010065
66 /* Clean previous status and enable the watchdog timer */
Michal Simek90663172014-02-12 14:41:19 +010067 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010068 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
69
70 iowrite32((control_status_reg | XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010071 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010072
Michal Simek90663172014-02-12 14:41:19 +010073 iowrite32(XWT_CSRX_EWDT2_MASK, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010074
Michal Simek90663172014-02-12 14:41:19 +010075 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +010076
77 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010078}
79
Michal Simekd14fd962014-02-12 14:34:32 +010080static int xilinx_wdt_stop(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010081{
Michal Simek5cf4e692014-02-12 14:34:33 +010082 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +010083 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +010084
Michal Simek90663172014-02-12 14:41:19 +010085 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010086
Michal Simek90663172014-02-12 14:41:19 +010087 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010088
89 iowrite32((control_status_reg & ~XWT_CSR0_EWDT1_MASK),
Michal Simek90663172014-02-12 14:41:19 +010090 xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010091
Michal Simek90663172014-02-12 14:41:19 +010092 iowrite32(0, xdev->base + XWT_TWCSR1_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +010093
Michal Simek90663172014-02-12 14:41:19 +010094 spin_unlock(&xdev->spinlock);
Maulik Jodhanib6bc4162017-08-07 13:24:22 +020095
96 clk_disable(xdev->clk);
97
Joe Perches27c766a2012-02-15 15:06:19 -080098 pr_info("Stopped!\n");
Michal Simekd14fd962014-02-12 14:34:32 +010099
100 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100101}
102
Michal Simekd14fd962014-02-12 14:34:32 +0100103static int xilinx_wdt_keepalive(struct watchdog_device *wdd)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100104{
Michal Simek5cf4e692014-02-12 14:34:33 +0100105 u32 control_status_reg;
Michal Simek90663172014-02-12 14:41:19 +0100106 struct xwdt_device *xdev = watchdog_get_drvdata(wdd);
Michal Simek5cf4e692014-02-12 14:34:33 +0100107
Michal Simek90663172014-02-12 14:41:19 +0100108 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100109
Michal Simek90663172014-02-12 14:41:19 +0100110 control_status_reg = ioread32(xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100111 control_status_reg |= (XWT_CSR0_WRS_MASK | XWT_CSR0_WDS_MASK);
Michal Simek90663172014-02-12 14:41:19 +0100112 iowrite32(control_status_reg, xdev->base + XWT_TWCSR0_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100113
Michal Simek90663172014-02-12 14:41:19 +0100114 spin_unlock(&xdev->spinlock);
Michal Simekd14fd962014-02-12 14:34:32 +0100115
116 return 0;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100117}
118
Michal Simekd14fd962014-02-12 14:34:32 +0100119static const struct watchdog_info xilinx_wdt_ident = {
120 .options = WDIOF_MAGICCLOSE |
121 WDIOF_KEEPALIVEPING,
122 .firmware_version = 1,
123 .identity = WATCHDOG_NAME,
124};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100125
Michal Simekd14fd962014-02-12 14:34:32 +0100126static const struct watchdog_ops xilinx_wdt_ops = {
127 .owner = THIS_MODULE,
128 .start = xilinx_wdt_start,
129 .stop = xilinx_wdt_stop,
130 .ping = xilinx_wdt_keepalive,
131};
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100132
Michal Simek90663172014-02-12 14:41:19 +0100133static u32 xwdt_selftest(struct xwdt_device *xdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100134{
135 int i;
136 u32 timer_value1;
137 u32 timer_value2;
138
Michal Simek90663172014-02-12 14:41:19 +0100139 spin_lock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100140
Michal Simek90663172014-02-12 14:41:19 +0100141 timer_value1 = ioread32(xdev->base + XWT_TBR_OFFSET);
142 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100143
144 for (i = 0;
145 ((i <= XWT_MAX_SELFTEST_LOOP_COUNT) &&
146 (timer_value2 == timer_value1)); i++) {
Michal Simek90663172014-02-12 14:41:19 +0100147 timer_value2 = ioread32(xdev->base + XWT_TBR_OFFSET);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100148 }
149
Michal Simek90663172014-02-12 14:41:19 +0100150 spin_unlock(&xdev->spinlock);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100151
152 if (timer_value2 != timer_value1)
153 return ~XWT_TIMER_FAILED;
154 else
155 return XWT_TIMER_FAILED;
156}
157
Bill Pemberton2d991a12012-11-19 13:21:41 -0500158static int xwdt_probe(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100159{
160 int rc;
Michal Simek8d6a1402014-02-12 14:41:25 +0100161 u32 pfreq = 0, enable_once = 0;
Michal Simekf06cdfd2014-02-12 14:34:34 +0100162 struct resource *res;
Michal Simek90663172014-02-12 14:41:19 +0100163 struct xwdt_device *xdev;
Michal Simek90663172014-02-12 14:41:19 +0100164 struct watchdog_device *xilinx_wdt_wdd;
165
166 xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
167 if (!xdev)
168 return -ENOMEM;
169
170 xilinx_wdt_wdd = &xdev->xilinx_wdt_wdd;
171 xilinx_wdt_wdd->info = &xilinx_wdt_ident;
172 xilinx_wdt_wdd->ops = &xilinx_wdt_ops;
173 xilinx_wdt_wdd->parent = &pdev->dev;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100174
Michal Simekf06cdfd2014-02-12 14:34:34 +0100175 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Michal Simek90663172014-02-12 14:41:19 +0100176 xdev->base = devm_ioremap_resource(&pdev->dev, res);
177 if (IS_ERR(xdev->base))
178 return PTR_ERR(xdev->base);
Michal Simekf06cdfd2014-02-12 14:34:34 +0100179
Michal Simek2e79a362014-02-12 14:41:21 +0100180 rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-interval",
181 &xdev->wdt_interval);
Michal Simek8d6a1402014-02-12 14:41:25 +0100182 if (rc)
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100183 dev_warn(&pdev->dev,
184 "Parameter \"xlnx,wdt-interval\" not found\n");
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100185
Michal Simek2e79a362014-02-12 14:41:21 +0100186 rc = of_property_read_u32(pdev->dev.of_node, "xlnx,wdt-enable-once",
187 &enable_once);
188 if (rc)
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100189 dev_warn(&pdev->dev,
190 "Parameter \"xlnx,wdt-enable-once\" not found\n");
Michal Simek2e79a362014-02-12 14:41:21 +0100191
192 watchdog_set_nowayout(xilinx_wdt_wdd, enable_once);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100193
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200194 xdev->clk = devm_clk_get(&pdev->dev, NULL);
195 if (IS_ERR(xdev->clk)) {
196 if (PTR_ERR(xdev->clk) != -ENOENT)
197 return PTR_ERR(xdev->clk);
198
199 /*
200 * Clock framework support is optional, continue on
201 * anyways if we don't find a matching clock.
202 */
203 xdev->clk = NULL;
204
205 rc = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
206 &pfreq);
207 if (rc)
208 dev_warn(&pdev->dev,
209 "The watchdog clock freq cannot be obtained\n");
210 } else {
211 pfreq = clk_get_rate(xdev->clk);
212 }
213
Michal Simek75b3c5a2014-02-12 14:41:22 +0100214 /*
215 * Twice of the 2^wdt_interval / freq because the first wdt overflow is
216 * ignored (interrupt), reset is only generated at second wdt overflow
217 */
Michal Simek8d6a1402014-02-12 14:41:25 +0100218 if (pfreq && xdev->wdt_interval)
Michal Simek90663172014-02-12 14:41:19 +0100219 xilinx_wdt_wdd->timeout = 2 * ((1 << xdev->wdt_interval) /
Michal Simek2e79a362014-02-12 14:41:21 +0100220 pfreq);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100221
Michal Simek90663172014-02-12 14:41:19 +0100222 spin_lock_init(&xdev->spinlock);
223 watchdog_set_drvdata(xilinx_wdt_wdd, xdev);
224
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530225 rc = clk_prepare_enable(xdev->clk);
226 if (rc) {
227 dev_err(&pdev->dev, "unable to enable clock\n");
228 return rc;
229 }
230
Michal Simek90663172014-02-12 14:41:19 +0100231 rc = xwdt_selftest(xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100232 if (rc == XWT_TIMER_FAILED) {
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100233 dev_err(&pdev->dev, "SelfTest routine error\n");
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530234 goto err_clk_disable;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100235 }
236
Michal Simek90663172014-02-12 14:41:19 +0100237 rc = watchdog_register_device(xilinx_wdt_wdd);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100238 if (rc) {
Michal Simek4c7fbbc2014-02-12 14:41:20 +0100239 dev_err(&pdev->dev, "Cannot register watchdog (err=%d)\n", rc);
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530240 goto err_clk_disable;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100241 }
242
Maulik Jodhanib6bc4162017-08-07 13:24:22 +0200243 clk_disable(xdev->clk);
244
Michal Simekd14fd962014-02-12 14:34:32 +0100245 dev_info(&pdev->dev, "Xilinx Watchdog Timer at %p with timeout %ds\n",
Michal Simek90663172014-02-12 14:41:19 +0100246 xdev->base, xilinx_wdt_wdd->timeout);
247
248 platform_set_drvdata(pdev, xdev);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100249
250 return 0;
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530251err_clk_disable:
252 clk_disable_unprepare(xdev->clk);
253
254 return rc;
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100255}
256
Michal Simek90663172014-02-12 14:41:19 +0100257static int xwdt_remove(struct platform_device *pdev)
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100258{
Michal Simek90663172014-02-12 14:41:19 +0100259 struct xwdt_device *xdev = platform_get_drvdata(pdev);
260
261 watchdog_unregister_device(&xdev->xilinx_wdt_wdd);
Shubhrajyoti Datta9d6b4ef2016-08-12 12:17:01 +0530262 clk_disable_unprepare(xdev->clk);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100263
264 return 0;
265}
266
Michal Simek6f671c62017-08-07 13:24:23 +0200267/**
268 * xwdt_suspend - Suspend the device.
269 *
270 * @dev: handle to the device structure.
271 * Return: 0 always.
272 */
273static int __maybe_unused xwdt_suspend(struct device *dev)
274{
275 struct platform_device *pdev = to_platform_device(dev);
276 struct xwdt_device *xdev = platform_get_drvdata(pdev);
277
278 if (watchdog_active(&xdev->xilinx_wdt_wdd))
279 xilinx_wdt_stop(&xdev->xilinx_wdt_wdd);
280
281 return 0;
282}
283
284/**
285 * xwdt_resume - Resume the device.
286 *
287 * @dev: handle to the device structure.
288 * Return: 0 on success, errno otherwise.
289 */
290static int __maybe_unused xwdt_resume(struct device *dev)
291{
292 struct platform_device *pdev = to_platform_device(dev);
293 struct xwdt_device *xdev = platform_get_drvdata(pdev);
294 int ret = 0;
295
296 if (watchdog_active(&xdev->xilinx_wdt_wdd))
297 ret = xilinx_wdt_start(&xdev->xilinx_wdt_wdd);
298
299 return ret;
300}
301
302static SIMPLE_DEV_PM_OPS(xwdt_pm_ops, xwdt_suspend, xwdt_resume);
303
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100304/* Match table for of_platform binding */
Jingoo Han9ebf1852014-05-07 17:42:22 +0900305static const struct of_device_id xwdt_of_match[] = {
Michal Simek8fce9b32013-05-31 07:56:34 +0200306 { .compatible = "xlnx,xps-timebase-wdt-1.00.a", },
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100307 { .compatible = "xlnx,xps-timebase-wdt-1.01.a", },
308 {},
309};
310MODULE_DEVICE_TABLE(of, xwdt_of_match);
311
312static struct platform_driver xwdt_driver = {
313 .probe = xwdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500314 .remove = xwdt_remove,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100315 .driver = {
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100316 .name = WATCHDOG_NAME,
317 .of_match_table = xwdt_of_match,
Michal Simek6f671c62017-08-07 13:24:23 +0200318 .pm = &xwdt_pm_ops,
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100319 },
320};
321
Axel Linb8ec6112011-11-29 13:56:27 +0800322module_platform_driver(xwdt_driver);
Alejandro Cabrerae9659e62011-06-02 22:13:11 +0100323
324MODULE_AUTHOR("Alejandro Cabrera <aldaya@gmail.com>");
325MODULE_DESCRIPTION("Xilinx Watchdog driver");
Michal Simek9419c072013-05-31 07:56:33 +0200326MODULE_LICENSE("GPL v2");